How passing variable as first parameter to add_action() work? - wordpress

I understand how actions work, something like this:
do_action() invokes all functions hooked to the action hook
add_action() hook the functions which we want to be invoked in some moment to an action hook but it won't invoked till do_action work
When i read in wordpress code for example add_submenu_page function there is something like this:
$hookname = get_plugin_page_hookname( $menu_slug, $parent_slug);
if (!empty ( $function ) && !empty ( $hookname ))
add_action( $hookname, $function );
My question is: what is the use of $hookname in add_action() and how to invoke the function ?

If you define a function:
function my_function() {
// Code here
}
And then make it an action using:
add_action( 'my_hook_name', 'my_function' );
It will be called whenever there is a call like:
do_action( 'my_hook_name' );
But the 'do_action()' is different to calling the function directly because it will also call any other functions that have been added to that hook. If you look at the docs for WordPress hooks you will see that there is a way to control the order of the actions when the 'do_action()' is called.

Related

wordpress own plugin has_shortcode does not work

I am using WP 4.3 and I created a plugin which is used only from one single page where I have the shortcode [my_sc].
I tried do realize like this:
add_action('init', 'my_enqu_script');
function my_enqu_script(){
global $post;
$content = $post->post_content;
if(has_shortcode($content, 'my_sc')){
wp_register_script(......);
wp_localize_script(......);
wp_enqueue_script('jQuery');
wp_enqueue_script('my_script');
}
function my_func(){
//do something
}
add_shortcode('my_sc', 'my_func');
}
Unfortunatly, that (has_shortcode) does not work. Any idea?
You shouldn't be wrapping the add_shortcode() bit in the my_enqu_script() function. Instead, add the shortcode, and then hook the other function into an appropriate hook:
function my_func(){
//do something
}
add_shortcode('my_sc', 'my_func');
function my_enqu_script(){
global $post;
$content = $post->post_content;
if ( has_shortcode($content, 'my_sc') ) {
wp_register_script(......);
wp_localize_script(......);
wp_enqueue_script('jQuery');
wp_enqueue_script('my_script');
}
}
add_action('wp_enqueue_scripts', 'my_enqu_script');
You should also consider using the wp_enqueue_scripts action hook, instead of init, since init is too early for global $post; to be in scope.
You just need to create your function and call the function add_shortcode().
e.g.
function my_function(){
echo 'Test';
}
add_shortcode('my_sc', 'my_function');
Also you used : instead of ; which might have been causing the failure.
You should add this to your functions.php file or create a seperate file (best practice) and include() it.

How to use conditional add_action for Wordpress

I am having a small issue where i need to call add_action method which is conditional. Basically here, first checking whether the plugin active or not by admin_init action. If true then i need another add_action call from that action.
Please find the scenario what i am looking for:
function check_some_other_plugin() {
if ( is_plugin_active('some-plugin.php') ) {
//if true then vc_before_init action will run
add_action( 'vc_before_init', 'vc_master_home_slider' );
}
}
add_action( 'admin_init', 'check_some_other_plugin' );
function vc_master_home_slider() {
//my stuff
}
I am really eager to know how this things can be possible. Thanks in advance.

How can I set a cookie based on page ID in wordpress?

Currently trying to have wordpress set a cookie based on particular pages.
I can set a general cookie from my functions file, and calling add_action() on the init hook.
/* functions.php */
function setCookies(){
global $post;
setcookie('test', 'it works');
var_dump($post->ID);
}
add_action( init, setCookies(), 10);
The var_dump is returning NULL.
Is there a hook that will execute in time to set a cookie, but late enough to have information from global $post;
The $post variable isn't set until you're inside the loop. Most themes have already generated output by the time you get there, so you won't be able to ever use that to set a cookie.
However, you should be able to hook into the wp action after the query is returned and set a cookie using your own custom loop. Try something like this:
function setCookies() {
global $wp_query;
if ($wp_query->have_posts()) {
$post_id = $wp_query->current_post;
setcookie('post_id', $post_id);
}
$wp_query->rewind_posts();
return;
}
add_action( 'wp', 'setCookies', 10);
See the Actions Run During a Typical Request and the WP_Query Class Reference in the codex.

Calling a plugin's function from functions.php

I am writing some code in the functions.php of my theme, now what I need to do is execute a function from inside another plugin and im wondering if its possible.
The plugin is wooevents pro and the function I need is as follows:
function order_contains_tickets( $order ) {
// Function code is in here
}
So my code will look something like this
if ( order_contains_tickets( $order ) ) {
// Execute code
}
Technically, the exact code you have would work, but it's also unsafe. In the event that the wooevents pro plugin were ever disabled, the code you added would be referencing a function that no longer exists. Thus, a better option would be to add a check to ensure that the function exists like so...
if( function_exists( 'order_contains_tickets' ) ) {
if( order_contains_tickets( $order ) ){
// Execute code
}
}
It should also be noted that this is dependent on the load order of the two functions. The wooevents pro function must be loaded prior to your function for it to work.

Wordpress Plugin: Show html only on standard page and not in admin area

I'm writing a plugin and I need to display a piece of text in the WP page, but not in the admin area. How can I do so?
I tried this in the construct:
add_action( 'init', array( $this, 'initPage' ) )
and then:
public function initPage() {
echo 'hello';
}
but the text is displayed also in the admin area. Is there a way to do this? It would be the opposite of the action admin_init I assume.
Proper way to handle it: is_admin()
http://codex.wordpress.org/Function_Reference/is_admin
if(is_admin()) { // do nothing } else {
// function you want to execute.
}
I solved this by adding it to a shortcode action. Like this:
add_shortcode( 'myPlugin', array( $this, 'shortcode' ) );
and:
public function shortcode( $atts ) {
return 'hello';
}
With the above code, 'hello' will only display on the front-end. Not sure if that's the cleaner way to do it, but does the job.
There is no "front-end-only" version of init, however you probably don't want to be doing any output at the init action anyway.
What exactly are you trying to do? Usually, you use an action hook for specific types of things, and causing output very early at something like "init" is rare and weird.

Resources