I Want to Assign Multiple Roles to a single user - php

I've found below two lines to be inserted somewhere in our theme to get the job done:
$addMemberToGroup = new WP_User($required_id);
$addMemberToGroup->add_role( $role );
But I dont know where to place these lines.
Does anybody know where exactly to put those?

On which action you want to assign user role, generally custom code need to put in theme's function.php file (if child theme then put in child theme's function.php file) but it all depends on your requirement.

$addMemberToGroup = new WP_User($required_id);
$addMemberToGroup->add_role( $role );
Put these lines to your theme functions.php
Create a function:
function add_new_role_to_user( $role ) {
$addMemberToGroup = new WP_User($required_id);
$addMemberToGroup->add_role( $role );
}
After that you need to hook this function
add_action( 'admin_init', 'add_new_role_to_user' );

Related

Replace a function of wordpress parent theme inside functions.php

There is not a specific answer for this exact issue so i am going to try this.
So in the parent theme of a wordpress website there is a php file named helpers-icons.php. The exact path for this file is /wp-content/themes/parent/inc/helpers/helpers-icons.php, and the content of that file is
function get_flatsome_icon($name, $size = null) {
if($size) $size = 'style="font-size:'.$size.';"';
return '<i class="'.$name.'" '.$size.'></i>';
}
This file is then inlcuded in functions.php of that parent theme.
Now i want to override a function inside and being more specific, just this line of code
return '<i class="'.$name.'" '.$size.'></i>'; to return '<span class="'.$name.'" '.$size.'></span>';
how could i do that on child theme without messing with the php files of parent?
Thanks in advance
EDIT
EDIT 2
EDIT - Different Approach
You can copy the file /inc/helpers/helpers-icons.php into your child theme (keep the folder structure - e.g. /child-theme/inc/helpers/helpers-icons.php). That way it will be called instead of the original file and you can change the function there.
(Original Answer) Removing Functions From Hooks:
You’ll need to de-hook the parent function and hook your child theme function instead by using remove_action() and remove_filter().
The one you use will depend on whether the function is attached to an action hook or filter hook in the parent theme.
To remove a function, use:
remove_action( 'init', 'parent_function' );
However, this won’t work on its own, you will need to attach this function to a hook which will fire after the hook which the parent theme function is attached to. This is because you can’t remove the action before it’s been fired.
function child_remove_parent_function() {
remove_action( 'init', 'parent_function', [priority level] );
}
add_action( 'wp_loaded', 'child_remove_parent_function' );
NOTE: If the parent_function() was loaded using a priority level, you have to unloaded by stating the same level as well. That’s why [priority level] is in square brackets.
As you may imagine, wp-loaded is a hook that comes after init, so remove_action() can really remove the parent_function() that was loaded to init.
Now you’re free to write whatever new function you’d like.
function child_function() {
// your function
}
add_action( 'init', 'child_function' );
Source and further reading: https://obsessive-coffee-disorder.com/how-to-override-parent-theme-functions-in-wordpress/

Modify WordPress plugin function in 'functions.php'

I need to change the value of a function from a WordPress plugin that I'm using.
If I do it in the original plugin file, it works perfectly, but I'd rather make it in file functions.php to avoid problems on plugin update.
I need to change this:
public static function loop_shop_per_page() {
return get_option( 'posts_per_page' );
}
Into this:
public static function loop_shop_per_page() {
return '-1';
}
How would I go to create a filter in file functions.php to do just that?
Without changing the core plugin code, I don't think you can achieve what you want.
Well written plugins usually have the apply_filters method called on anything that might need to be tweaked by a theme developer. As such, the plugin code should look something like this:
// Using apply_filters() to allow overriding of values...
public static function loop_shop_per_page() {
$value = get_option( 'posts_per_page' );
return apply_filters('filter_loop_shop_per_page', $value);
}
Then, in your functions.php file, you'd be able to change the value on for that by adding a filter like so:
add_filter('filter_loop_shop_per_page', 'mytheme_loop_shop_per_page');
function mytheme_loop_shop_per_page($value){
return -1;
}
Unfortunately, unless you edit the plugin code, you probably won't be able to do what you want. You could always search the plugin for apply_filters to see if there's anything else you might be able to tweak to achieve the same results.

Override metaboxes from parent theme in child theme in WordPress

I have a parent theme in WordPress that adds many functions and features into WordPress, including several metaboxes.
In my child theme, I have added some new custom post types into WordPress, so I am trying to modify the meta boxes to include them in the new custom post types.
Here is the parent theme functions.php file: http://pastebin.com/G1CVM6U2
My thought was that I would simply need to add code into my child theme that would remove the action from the parent theme that adds in the metaboxes, and then copy the metabox files into my child theme, modify them as needed, and then use add_action to pull in a new function that calls those metabox files.
However, I am having a hell of a time even being able to remove the action from the parent theme.
I have tried several variations of the following in my child theme functions.php:
remove_action('init', 'add_metaboxes');
remove_action('add_meta_box', 'add_metaboxes');
It does not remove the metaboxes though, as when I check the page/post editor, they are still visible.
Without running the remove_action, I cannot then do a new "add_action" to call in the modified metabox files. When I try to do so, I get a white screen of death due what I guess is the same function being redeclared.
I suspect that because the action in the parent theme is being called as part of a class, the process of over-riding this in the child theme might be differet?
Would appreciate some help on this one.
Sample 1: Remove a normal named add action
add_action('after_setup_theme', 'remove_meta_boxes');
function remove_meta_boxes(){
remove_action('add_meta_box', 'add_metaboxes', 10);
}
Sample 2: remove a function passed by reference
the add_action looks like this:
class ab{
function __construct(){
add_action('init', array( &$this, 'add_metaboxes' ));
}
}
$x=new ab;
The problem here is that wordpress will prepend a random identification string to the function name e.g. (a3898orj34930add_metaboxes), this is random every time so what we need to do is use strpos to find the name in the key. I have created a custom function for this.
function remove_anon_filters( $name, $functionname, $priority){
global $wp_filter;
foreach ( $wp_filter[$name][$priority] as $key => $data) {
if( stripos($key, $functionname ) ){
remove_action( $name, $key, $priority );
}
}
}
We than then hook into after theme set up to remove the parent theme action.
add_action('after_setup_theme', 'remove_parent_meta_boxes');
function remove_parent_meta_boxes(){
remove_anon_filters ( 'init', 'add_metaboxes', 10);
}

Wordpress filter not being added

I have a plugin that uses apply_filters like this:
$additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);
In my theme's functions.php, I do:
function addAttachmentMeta($additionalFields) {
return $addtionalFields;
}
add_filter( 'attachment_meta_add_fields', 'addAttachmentMeta', 1, 1 );
But the function addAttachmentMeta never runs.
How can I alter my apply or add filter statements to make it so that addAttachmentMeta gets called?
Edit:
This is a custom plugin that I wrote based off tutorials on how to add additional attachment meta fields. The whole source is here: http://pastebin.com/7NcjDsK5. As I mentioned in the comments, I know this is running and working because I can add additional fields in this plugin file, but not by using the filters because the filter doesn't get added.
I can see var_dumps before and after the apply_filters statement, but the function I've pointed to with add_filter never gets called.
According to the order WordPress' core loads, function.php gets called after all plugins are loaded and executed.
You need to make sure the apply_filters() in your plugin runs AFTER your add_filter() is called. Otherwise at the point where your filters are 'applied', add_filter() simply hasn't been called yet.
What you could do is use a hook to make that part of your plugin run after functions.php has loaded. You could use the add_action('after_setup_theme', 'function_name') hook.
Wrap the last three lines of your plugin file inside a function and execute it after functions.php runs.
function addAttachmentMeta() {
$additional_fields = array();
$additional_fields = apply_filters('attachment_meta_add_fields', $additional_fields);
$am = new Attachment_Meta( $additional_fields );
}
add_action('after_setup_theme', 'addAttachmentMeta');

Filter in WP child theme

I have this filter added in functions.php of my childtheme:
function tp_edit_ad_columns( $columns ) {
$columns['test'] = 'Something in title...';
return $columns;
}
add_filter('manage_edit-'.APP_POST_TYPE.'_columns', 'tp_edit_ad_columns', 11 );
Can anyone explain why it don't work? If this same function/filter is put in functions.php of the main theme it works.
Thank you in advance.
This is not a core filter.
So probably it is created in the parent theme.
For some reason it is turned down by your child theme.
Search the parent theme for apply_filter or manage_edit to find when this filter is applied.
From there you'll have to give moreinformation

Categories