I need to check if a user have capabilities to do something in the initialization of a plugin.
I tried with current_user_can() function, and WP_User class, but dont work in these scope.
The question is: I need to check if a user have capabilities in wp-admin, to let he see a specific menu or just to load the content of the plugin.
But how? Both methods above generates a php error.
You should hook your function to run after plugins have loaded
function my_special_function() {
if ( current_user_can( 'do_whatever' ) )
// do your thing
}
add_action( 'plugins_loaded', 'my_special_function' );
Source: http://core.trac.wordpress.org/ticket/23861
Related
I'm using "Advanced Scripts plugin" to modify a function of other plugin, the fuction I'm trying to modify is wrappd with if( !function_exists('some_function') ).
the function inside the plugins is like this
if( !function_exists('send-invoice') ){
function send-invoice(){
//The Plugin Invoice
}
}
This is what I did
function send-invoice(){
//My Custom Invoice
}
add_action('init', 'send-invoice');
How can I make sure that my code runs before the plugin codes?
The plugin load before the theme, I tried plugin-loaded hook but nothing changed
You can to use the anonymous function for example:
add_action('init', function() {
//code here
});
More detail is here
Or use another hook muplugins_loaded:
function send-invoice(){
//My Custom Invoice
}
add_action('muplugins_loaded', 'send-invoice');
If nothing else work for you you can create a custom plugin, name it something like "aaamyplugin" and just insert there a single .php file with the function you are trying to override. This is the easiest (not cleanest) way to make sure your code overrides the plugin functions.
The reason for this is because Wordpress plugin loading order is simply alphabetical, that means that everything named before the plugin you are trying to override, get loaded first.
The cleanest way would be to look into the source code of the plugin to understand how it does what it does. Like: when does it load that file that contains the function you are trying to override? That's the important question to answer if you want to go with clean way
I'm not that new with WordPress and PHP but I don't understand a function anymore. Which function? This here:
add_filter( 'wpas_ticket_status_updated', 'test' );
function test( $post_id, $status, $updated ) {
error_log( 'updated' );
}
My plan is to log out a ticket when it's getting updated to export the log for testing purposes. So I've took a look into the developer documentation of Awesome Support and found the hook above:
https://developer.getawesomesupport.com/documentation/hooks-and-filters/wpas_ticket_status_updated/
So I've implemented it into my function.php (the normal way) and created a ticket. So the status needs to be created. After this I've changed the status of the ticket to in progress but I got nothing. Nothing about the creation and nothing about the status change.
What I'm doing wrong? I don't get it...
According to the documentation of the plugin, you are mixing filters and actions. Based on your provided code you are trying to trigger your test function once the filter wpas_ticket_status_updated is called. However, this function is not a filter but an action, therefore your hook is never to be called.
You should therefore change your code to add_action('wpas_ticket_status_updated', 'test') or find a related filter to call your desired test function.
Creating a wordpress plugin and need this plugin to automatically add posts to wordpress when it installs. What is the better solution to do that?
You should use the register_activation_hook to carry out any custom functions (like adding posts) after the plugin is initialised.
function myplugin_activate() {
// Add your posts here...
}
register_activation_hook( __FILE__, 'myplugin_activate' );
That way, once the plugin is installed, and activated, myplugin_activate() will fire, and carry out any logic you put there.
See: https://codex.wordpress.org/Function_Reference/register_activation_hook for more information.
I am not very old on Wordpress.
I am developing a website which has a Third party plugin having class Plugin_Class and there are some filters in the construct of it. I want to hook my additional function to filter Plugin_filter_1 from functions.php.
I have tried with the syntax add_filter( 'Plugin_filter_1', array('Plugin_Class', 'my_function_defined_in_functions_php' ) );,
it gives me error even if the class is visible ( checked with get_declared_classes() )
The detailed answer will be appreciated
Thanks for your help
Regards
Possibly the issue is that your second parameter for add_filter is an array with Plugin_Class as the first element- this means wordpress will try to fire PluginClass::my_function_defined_in_functions_php, which doesn't exist.
Instead, just pass it a string of 'my_function_defined_in_functions_php'.
If this doesn't work with just this edit, show us the code in the plugin class- including the class name and entire construct method. There are more possible issues.
I'm quite new to WP. Task is to develop a plugin for oauth authentication on one of not popular openID providers. I did the same for CodeIgniter project, but WP is a CMS and is little bit complex for me to understand. In Codeigniter i check authorisation before each action. In WP i need a hook which uses for it... before each page printing, or maybe.. it would be right to say before each action in terms of frameworks. What is this hook's name?
Last hook before loading the template is template_redirect
You can use it like this:
function my_function(){
// your code goes here
}
add_action( "template_redirect", "my_function" );
You can use init hook. It will be performed before element or HTML code. It's also useful to manage POST and GET variables. The syntax is something like this:
function yourfunction() {
dosomething();
}
add_action('init', 'yourfunction');
A list of all available hooks can be found here: https://codex.wordpress.org/Plugin_API/Action_Reference
Information about Hooks: https://codex.wordpress.org/Plugin_API#Hooks.2C_Actions_and_Filters
Other hooks must be suggested and will be added in a future release if is a good suggestion.
Or you'd have to edit the core files ;)
You mean a hook when all wordpress function will available but before any output including headers sent?
Well hook your function on init. That will call when visiting site. If you want this hook only for admin area then it is admin_init.