2

I am working on a Wordpress project where I need to dynamically create a function (depending on which kind of template is used for a page or post) that retrieves the comments of each page in question.

So let's say I have pages within Wordpress with the IDs 100, 110, 120, 130, 140, 150 and out of these 3 are using the template called "blog" (e.g.: 100, 130 and 150).

So in order to retrieve the comments from these 3 pages with AJAX I need to create a function for each of them:

function GetComments100() { #### }
function GetComments130() { #### }
function GetComments150() { #### }

Here's the function code I need to create individually for each page (and which goes in between the function brackets above (instead of the ####):

$defaults = array( 'order' => 'DESC', 'post_id' => $functionID, 'post_type' => 'page', 'count' => false ); 
$comments = get_comments($defaults);
foreach($comments as $comment) :
echo "<div class='table-row' style='margin-bottom:1px'><div class='table-cell-1' style='width:110px;'>".$comment->comment_author.":</div><div class='table-cell-2'style='width:870px;'>".$comment->comment_content."  <em><a>".$comment->comment_date." ... ".get_the_title($comment->comment_post_ID)." (".$comment->comment_post_ID.")</a></em></div></div>";
endforeach;
die($results);

In order to get the pages I use a loop-function which gives me the page ID as a variable (in my case its $functionID (also included in the array of my function above)).

I have already managed to dynamically create the functions with the following lines of code (I know "eval" is not a good choice but I didn't find any other solution):

$string = 'function ' . $functionName . "() { 
####
}";
eval($string);

Now instead of the #### I need to integrate the actual function code starting with "$defaults = array(..." but obviously it has to be completely converted to a string - which is what I am struggling with.

Any help would be appreciated (again, I know using "eval" is not nice but so far I didn't find any other solution for this)

1
  • Turn that block of code into function x($functionID). Then change to function GetComments100() { x(100); }. Will it work? I don't know a thing about WordPress.
    – Joker_vD
    Commented Jun 11, 2013 at 10:29

2 Answers 2

0

Have you tried using nowdoc for the function body? If you just need expanding $functionName, you can try something like this:

      $string="function {$functionName}(){".<<<'END'
$defaults = array( 'order' => 'DESC', 'post_id' => $functionID, 'post_type' => 'page', 'count' => false ); 
$comments = get_comments($defaults);
foreach($comments as $comment) :
echo "<div class='table-row' style='margin-bottom:1px'><div class='table-cell-1' style='width:110px;'>".$comment->comment_author.":</div><div class='table-cell-2'style='width:870px;'>".$comment->comment_content."  <em><a>".$comment->comment_date." ... ".get_the_title($comment->comment_post_ID)." (".$comment->comment_post_ID.")</a></em></div></div>";
endforeach;
die($results);
}
END;

eval($string);
2
  • thanks, that seems to be the right direction. i've tested it but it didn't work most probably because my php version is prior 5.3... :-( would it work with heredoc as well?
    – bender2601
    Commented Jun 11, 2013 at 17:01
  • Yes, but escaping the dollar signs (\$defaults=...), etc.
    – hammurabi
    Commented Jun 11, 2013 at 17:25
0

I don't understand why you don't use one function per Template with a parameter like this:

public function getBlogComments($id){
    //...
}

or one function which check the used Template

public function getComments($id){
    // get Template of $id
    //...
}
1
  • I can't use it that way because I am retrieving the comments from these three (in this example) pages altogether on another single page (incl. a comment form for each of these individual comment-blocks). As I use AJAX to write and retrieve comments I need an individual function for each. I am sure I could combine all three comment sections within one function - but I want them to be treated individually (so - for example - if I write a comment for pageID 130 I only want the comment-block for pageID 130 to be refreshed)
    – bender2601
    Commented Jun 11, 2013 at 20:39

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.