Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

PHP - Can I use eval to call a number of functions

  • 30-10-2011 12:15pm
    #1
    Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭


    I am experimenting with adding debug info to the header of a WordPress theme, using the add_action() hook.

    I want to call all the WordPress conditional tags.
    Currently I have code like:
    [php] // WordPress conditional tags.
    echo "WordPress conditional tags information.\n";
    echo ' is_home(): ', is_home() ? 'true' : 'false', "\n";
    echo ' is_front_page(): ', is_front_page() ? 'true' : 'false', "\n";
    echo ' is_page(): ', is_page() ? 'true' : 'false', "\n";
    echo ' is_page_template(): ', is_page_template() ? 'true' : 'false', "\n";
    //etc...[/php]
    Is there a way to use eval in a loop to call these functions?
    Something like:
    [php]$fns = array('is_home', 'is_front_page', 'is_page', 'is_page_template');
    foreach ($fns as $fn) {
    echo " $fn(): ", eval("return ${fn}()") ? 'true' : 'false', "\n";
    }[/php]


Comments

  • Registered Users, Registered Users 2 Posts: 3,141 ✭✭✭ocallagh


    Have a look at array_walk, array_map and call_user_func


  • Registered Users, Registered Users 2 Posts: 6,571 ✭✭✭daymobrew


    ocallagh wrote: »
    Have a look at array_walk, array_map and call_user_func
    call_user_func did the job.
    The code is much shorter now:
    [php] echo "WordPress conditional tags information.\n";
    $conditional_tags = array( 'is_home', 'is_front_page', 'is_page', 'is_page_template', 'is_single',
    'is_singular', 'is_category', 'is_archive', 'is_author', 'is_search', 'is_404',
    'is_paged', 'is_attachment', 'is_feed', 'in_the_loop', 'is_multi_author',
    'is_date', 'is_year', 'is_month', 'is_day', 'is_time',
    'is_tag', 'is_tax', 'is_sticky',
    'is_comments_popup', 'comments_open', 'pings_open',
    'is_admin');
    foreach ($conditional_tags as $cond_tag) {
    echo " $cond_tag(): ", call_user_func($cond_tag) ? 'true' : 'false', "\n";
    }
    // Template tags that do not return true/false.
    $other_tags = array('get_post_type');
    foreach ($other_tags as $cond_tag) {
    echo " $cond_tag(): ", call_user_func($cond_tag), "\n";
    }[/php]


Advertisement