0

Im creating a custom function for my wordpress website that will add a review section below the post content and i need to insert this function from another another file into a custom function that i added to my functions.php. I need to get this piece of code $buffy .= $this->get_review(); from a different file to work in this function:

function content_injector($content) { 
    global $wp_query;   
    $postid = $wp_query->post->ID;

    if (is_single((get_post_meta($postid, 'top_ad', true) != 'off' )))  {
        $top_ad = do_shortcode('[td_ad_box spot_name="Ad spot -- topad"]');
    } 

    if (is_single((get_post_meta($postid, 'bottom_ad', true) != 'off' ))) {
        $bottom_ad = do_shortcode('[td_ad_box spot_name="Ad spot -- topad"]');
    }

    if (is_single()) {
        $review = //HOW DO I ADD THAT get_review CODE HERE?
        $custom_share = '<div id="title-deel"><h4 class="block-title"><span>DEEL</span></h4></div>' . do_shortcode('[ssba]');
        $facebook_comments = '<div id="title-reageer"><h4 class="block-title"><span>REAGEER</span></h4></div>' . '<div class="fb-comments" data-href="' . get_permalink() . '" data-colorscheme="light" data-numposts="5" data-mobile="false" data-width="700"></div>';
    }

    $content = $top_ad . $content . $bottom_ad . $custom_share . $facebook_comments;
    return $content; 
}

add_filter('the_content', 'content_injector');

As you can see i need to add the get_review function to $review, but i cant make it work on my own. How to make this work?

4
  • Have you considered include()? Commented Jan 9, 2014 at 23:23
  • Yeah couldn't get the include funtion to get the get_review function to work! Commented Jan 9, 2014 at 23:25
  • You should place all your functions in the functions.php file to make it easier to work with on WordPress. However, include or require should do the job? Commented Jan 9, 2014 at 23:28
  • Well the $buffy .= $this->get_review(); code is located in a theme file similar to single.php, a file that constructs the lay out of post pages. Commented Jan 9, 2014 at 23:33

1 Answer 1

2

Use include to include the file before using any methods from that file.

include("file_with_functions.php");

OR

Create a class (with filename same as classname).

Include the file.

Create an instance of the class.

Access the method in the class through the object

Sign up to request clarification or add additional context in comments.

7 Comments

additionally if you want to ensure the file is only included once, use include_once(). also, depending on the type of error you want thrown if the file doesn't exist, look into require() and require_once()
Where in my fuction do i need to add the include function? I cant add it after right after $review = , right?
you typically add the include to the top of the file though not necessarily. Then you could create an instance and call the method from within your content_injector method.
@raj, can u give me an example? I really have no idea how the code should look like...
what is the class in which this function is located?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.