• Resolved cuongpham259

    (@cuongpham259)


    Hi Jose,

    Thanks for creating a great plugin.

    I see you updated a HOOK to version 0.0.9. But I can’t find instructions on how to use this hook.

    Can you share more about it?
    Can I pass post_id, post_catelogy data to process with Ajax?

    Thanks a lot!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Jose Mortellaro

    (@giuse)

    Hi @cuongpham259,

    thanks to you!

    The hooks are mentioned in the changelog to track the code changes, but they are still not officially explained because their use is still under development. They will be used mainly by the PRO version.

    However, you can pass the ID of the Content No Cache element, and an array containing some information. You can’t pass the ID of the current Post object only using that hook.

    Here you have an example:

    add_action( 'content_no_cache_before_sending_content', function( $cnc_id, $arr ) {
    
    /**
    
    * It fires just before printing the content during the Ajax call. Most of global variables will not work here.
    
    *
    
    * @param int $cnc_id ID of the Content No Cache element.
    
    * @param array $array Array including some information like the user headers,
    
    *
    
    */
    
    // $arr['headers'] is the array of headers sent by the browser of the current user if they are logged in.
    
    ?>
    
    <div class="before-content-no-cache-during-ajax">Some stuff here</div>
    
    <?php
    
    }, 10, 2 );

    Getting the post ID is possible but it’s a little tricky. I will try to explain it, but this is not the aim of the free version.

    To make work your custom code where you want access to the post ID, you need to hook to the filter ‘content_no_cache_var_[var-name]’, and update your shortcode replacing the ID with the name of your custom function and the variables that you want to fetch, e.g. [content_no_cache id=”my_cnc_function” vars=“[var-name]”]

    [var-name] is the name of the variable that you want to get.

    If you need more variables, separate them with a comma.

    In your case you want the post ID, so a good name to describe your vars is post_id, and your shortcode would be:

    [content_no_cache id=”my_cnc_function” vars=”post_id”]

    Then, according to this example, you will find the variable value in the super global variable $_POST as $_POST[‘post_id’] in your custom function my_cnc_function.

    For example:

    add_filter( 'content_no_cache_var_post_id', function( $post_id ){
      // It fires on the page before the Ajax call.
      global $post;
      return $post->ID;
    } );
    
    function my_cnc_function() {
      // It fires during the Ajax call (wp_doing_ajax() = true).
      echo 'testing function; post ID: ' . esc_html( $_POST['post_id'] );
    }

    In this case, you don’t use a Content No Cache element, but you call your custom PHP function my_cnc_function to the shortcode.
    Inside your custom function of course you can call what you need.

    I know it may appear tricky. I hope it helps.

    Have a great day!

    Jose

    Thread Starter cuongpham259

    (@cuongpham259)

    Hi @giuse

    Thanks for the thorough explanation. But it’s true that it’s appear tricky.

    I posted this question because I encountered this situation.
    I want to use a Plugin to load related articles in an “category”

    I can’t display this list, because before displaying the content using the Plugin, No value “term_id” is passed in. So it will return empty.

    Or in a specific post, related posts cannot be displayed without the passed “post_id” and “term_id” values.

    I have tried with custom php code in the past, but code not working. This hook is clearly a lifesaver for future solutions.

    Hope you can make it simpler with the paid version, more documents updated in the future.

    Thank Jose

    • This reply was modified 2 years, 6 months ago by cuongpham259.
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘action hook ‘content_no_cache_before_sending_content’’ is closed to new replies.