•   add_filter( 'the_content', 'my_function' );  
    
      function my_function( $post_entry ){
        // check if my POST vars are set
        // retrieve POST variables
        // more code to modify $post_entry
    
        return $post_entry
      }
    
      function my_shortcode(){
        return "/ form code /";
      }
    
      add_shortcode( 'the_shortcode', 'my_shortcode' );

    As you can see I’m “catching” POST variables in the_content filter. It would obviously be better to catch them in an action hook but I must use the_content filter as I modify it with the POST variables. Any ideas how to catch POST variables somewhere else and then pass them to my_function (the_content filter callback) ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Each hook is only passed the data that it is set up for (where it is called). Action hooks don’t return anything, and filters can only modify the first parameter.
    You can code your plugin as a class, hook into 'init' to process POST variables and set class variables as needed, then ‘the_content’ filter function can reference the class variables. But it’s pretty much the same thing either way.
    Make sure your form variables are uniquely named so they don’t conflict with other plugins or core variables.

    It isn’t clear why you included the shortcode in your example code, but if you are doing a shortcode with a form, it should be processed in the same page, by the shortcode handler. That is, if it doesn’t use an actual action attribute to send the user to a specific page. If this shortcode is where the POST variables come from, then do you need 'the_content' filter?

    In your example, you wouldn’t pass the POST values to the function, you’d just reference then in your function.

    As an example…

    add_filter( 'the_content', 'my_function' );  
    
      function my_function( $post_entry ){
        if ($_POST ['some_value'] == 'some_setting') {
            $post_entry = str_replace ('blue', 'red', $post_entry);
        }
    
        return $post_entry
      }

    Hi @dreamsinbinarycode,

    Filters the post content: apply_filters( 'the_content', string $content )

    When using this filter, it’s important to check if you’re filtering the content in the main query with these conditions is_main_query() and in_the_loop(). The main post query can be thought as the primary post loop that displays the main content for a post, page or archive. Without these conditionals you could unintentionally be filtering the content for custom loops in sidebars, footers, or elsewhere:

    add_filter( 'the_content', 'filter_the_content_in_the_main_loop', 1 );
     
    function filter_the_content_in_the_main_loop( $content ) {
     
        // Check if we're inside the main loop in a single Post.
        if ( is_singular() && in_the_loop() && is_main_query() ) {
            return $content . esc_html__( 'I’m filtering the content inside the main loop', 'wporg');
        }
     
        return $content;
    }

    `

    Let me know if my answer can help you resolve your issue.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Pass POST data to the_content filter function?’ is closed to new replies.