Title: Pass POST data to the_content filter function?
Last modified: July 15, 2021

---

# Pass POST data to the_content filter function?

 *  [dreamsinbinarycode](https://wordpress.org/support/users/dreamsinbinarycode/)
 * (@dreamsinbinarycode)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/pass-post-data-to-the_content-filter-function/)
 *     ```
         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)

 *  [Joy](https://wordpress.org/support/users/joyously/)
 * (@joyously)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/pass-post-data-to-the_content-filter-function/#post-14666933)
 * 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?
 *  [catacaustic](https://wordpress.org/support/users/catacaustic/)
 * (@catacaustic)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/pass-post-data-to-the_content-filter-function/#post-14667053)
 * 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
         }
       ```
   
 *  [LitExtension_Lucas](https://wordpress.org/support/users/lucaslitextension/)
 * (@lucaslitextension)
 * [4 years, 11 months ago](https://wordpress.org/support/topic/pass-post-data-to-the_content-filter-function/#post-14668258)
 * Hi [@dreamsinbinarycode](https://wordpress.org/support/users/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.
    -  This reply was modified 4 years, 11 months ago by [LitExtension_Lucas](https://wordpress.org/support/users/lucaslitextension/).

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Pass POST data to the_content filter function?’ is closed to new replies.

## Tags

 * [hook](https://wordpress.org/support/topic-tag/hook/)
 * [post](https://wordpress.org/support/topic-tag/post/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 4 participants
 * Last reply from: [LitExtension_Lucas](https://wordpress.org/support/users/lucaslitextension/)
 * Last activity: [4 years, 11 months ago](https://wordpress.org/support/topic/pass-post-data-to-the_content-filter-function/#post-14668258)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
