2

I am trying to have 2 php functions automatically inserted in every post for my CPT. The problem I am having is that even though I've found a way to add text, Im not sure how to add the php functions cause with the code I have it adds it as regular text to the post.

This is what I have in my functions file ---

    add_filter( 'default_content', 'my_editor_content', 10, 2 );

    function my_editor_content( $content, $post ) {

        switch( $post->post_type ) {
            case 'property':
 $content = "<div>";
                $content . "<?php if ( class_exists( 'MRP_Multi_Rating_API' ) ) {
        MRP_Multi_Rating_API::display_rating_result( array(
                'rating_item_ids' => 2,
                'show_count' => false,
                'result_type' => 'value_rt',
    'no_rating_results_text' => 'Not Rated'
        ) ); } ?><?php if ( class_exists( 'MRP_Multi_Rating_API' ) ) {
        MRP_Multi_Rating_API::display_rating_result( array(
                'rating_item_ids' => 5,
                'show_count' => false,
                'result_type' => 'overall_rt',
    'no_rating_results_text' => 'Not Rated'
        ) );
    } ?>";
 $content = "</div>";
            break;
            default:
                $content = "your default content";
            break;
        }

        return $content;
    }

How can I fix this so my functions work and is it possible to make it add to every post not just the new ones created?

Thanks

1

I'm sure you want to use the_content filter instead of the default_content filter (already mentioned by @jgraup) because what happens if the generated HTML changes? Then you're stuck with it in your content. It's better to add it dynamically. Here's one suggestion:

add_filter( 'the_content', function( $content)
{
    if( ! in_the_loop() )
        return $content;

    if( 'property' !== get_post_type() )
        return $content;

    $args = [
        'rating_item_ids'           =>  2,
        'show_count'                => false,
        'result_type'               => 'value_rt',
        'no_rating_results_text'    => 'Not Rated',
        'echo'                      => 0,
    ];

    if ( class_exists( 'MRP_Multi_Rating_API' ) ) 
    {
        $content .= MRP_Multi_Rating_API::display_rating_result( $args ); 

        // Modify arguments for the second run
        $args['rating_item_ids'] = 5;
        $args['result_type']     = 'overall_rt';

        $content .= MRP_Multi_Rating_API::display_rating_result( $args ); 
    }

    return $content;
} );

Notice that we use the in_the_loop check (here I link to the question by @PieterGoosen and the answer by @gmazzap), because I assume you don't want this added to every content part of your site.

You also don't need to check twice for the existince of the same class.

Note that this has one big assumption, namely the echo attribute ;-)

  • He wants to insert the output so it becomes searchable by the default WP search... which means both methods are a bust. If it's searchable then you would already have the values / display rendered in advance. – jgraup Dec 20 '15 at 20:36
  • @jgraup Yeah I need the text to be present within the_content. This is the only use I make of "the_content" cause everything else I hard coded within my single.php files. So there's all type of stuff being displayed/rendered within my posts, but it doesnt come from the_content so I am unable to search for it. The functions echo text for ratings such as "decent". When searching that word though, nothing comes up. – Rich Dec 20 '15 at 20:38
  • @jgraup both of your codes work but it's not going into the_content like I need, it is being added after. If I do it with plain text then it seems to work and even shows up on the post edit. Im not sure how to workaround this with functions though. – Rich Dec 20 '15 at 20:40
  • If I asked you how to insert '123' into 'abcdefghijklmnopqrstuvwxyz', how would you answer that? – jgraup Dec 20 '15 at 20:42
  • I see, what information do you need from the generated HTML code and where is that data stored in the database (custom table?) Maybe you could do some extra joins for the search? @mygm26 That might be better served with a, new question, as suggested by jgraup. – birgire Dec 20 '15 at 20:44
1

Use shortcodes to add php functionality inside the content area. Or, ob_start() and ob_get_clean() to capture the output and append to the $content you are returning.


The "the_content" filter is used to filter the content of the post after it is retrieved from the database and before it is printed to the screen.

add_filter( 'the_content', 'my_the_content_filter', 20 );

function my_the_content_filter($content) {

    $post = $GLOBALS['post'];

    ob_start();

    switch($post->post_type) {
        case 'property':
            if(class_exists('MRP_Multi_Rating_API')) {
                MRP_Multi_Rating_API::display_rating_result(array(
                                                                'rating_item_ids'        => 2,
                                                                'show_count'             => false,
                                                                'result_type'            => 'value_rt',
                                                                'no_rating_results_text' => 'Not Rated',
                                                            ));

                MRP_Multi_Rating_API::display_rating_result(array(
                                                                'rating_item_ids'        => 5,
                                                                'show_count'             => false,
                                                                'result_type'            => 'overall_rt',
                                                                'no_rating_results_text' => 'Not Rated',
                                                            ));
            }
            break;
    }

    $extra = ob_get_clean();

    if( ! empty($extra)) {
        return "<div>" . $content . $extra . "</div>";
    }

    return $content;
}

If you want to just insert stuff into content use str_replace on the $post.

$post = get_post(37);

// Update post 37
$my_post = array(
    'ID'           => $post->ID,
    'post_content' => str_replace('round hole', 'square peg' . ' round hole', $post->post_content),
);

// Update the post into the database
wp_update_post($my_post);
  • Thanks but is there a way so that is actually inserted within $content? It seems to append it after the_content, but because of the wordpress search function only searches the_title and the_content, I need the functions to be in the_content so I can be able to search for it from the search form. – Rich Dec 20 '15 at 20:27
  • There are other ways to insert things into the content. And, as with search, you'll need to create another question for that because it's beyond the scope of this question. – jgraup Dec 20 '15 at 20:37
  • $post = get_post($ID); // Update post 37 $my_post = array( 'ID' => $post->ID, 'post_content' => str_replace('round hole', 'square peg' . ' round hole', $post->post_content), ); // Update the post into the database wp_update_post($my_post); Like this? How do I add the functions? – Rich Dec 20 '15 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.