WordPress.org

Ready to get started?Download WordPress

Ideas

Support echo or return in template functions

  1. jdandrea
    Member

    12345

    Sometimes I'd like to be able to combine the results of a few different template functions. If I could get the value returned (versus echoed outright) that would be possible out-of-the-box. Some WP plugins offer this same kind of toggle as well.

    Posted: 7 years ago #
  2. jdandrea
    Member

    12345

    Ahh! I see that wp_list_pages takes an echo param ... but I should clarify what I was referring to: wp_list_categories does _not_ accept an echo param.

    Posted: 7 years ago #
  3. jdandrea
    Member

    12345

    Here's another twist ... what if the list could be returned AS a list/array? Even cooler!

    So, to recap, I imagine that any function that outputs something resembling a list should do so under-the-hood, but then you (points toward reader) get to specify one of three ways to handle it!

    • Return as a list/array
    • Return as a string
    • Echoed (the default)

    Whatcha think about that?

    Posted: 7 years ago #
  4. phKU
    Member

    12345

    This is exactly what I'm most missing with our great wp. Currently, template function APIs are quite inconsistent with the way they bring back (or not) and echo the data. This is really frustrating in customizing a layout. Actually I'm bound to use my private and self made plug-in. This approach make each significant wp upgrade a real pain like recently with database changes in v.2.1.

    I suggest that each template function would accept arrays args as input (like added with v.2.1 in 'wp_list_archives') and get an option to send back an array filled with the complete relevant data. This response array doesn't need to be filtered (e.g. which cat or page to bring back) since it would then be easy to post process the data with some snap coding.

    Posted: 7 years ago #
  5. Ketwaroo
    Member

    12345

    Really a good idea. All template functions should be able have the same arguments.
    Would be a good idea to add a '$before' and '$after' for each element.
    Or even a '$child_before' and '$child_after' for child elements

    i already modified some functions

    edit-- BTW, i give it a 5 star rating... not a measly 3.5

    // yes even that one...
    function the_search_query($echo=1)
    {
    	global $s;
    	if(!$echo)
    		return attribute_escape(stripslashes($s));
    	echo attribute_escape(stripslashes($s));
    }
    
    function wp_list_categories($args = '') {
    	if ( is_array($args) )
    		$r = &$args;
    	else
    		parse_str($args, $r);
    
    	$defaults = array('show_option_all' => '', 'orderby' => 'name',
    		'order' => 'ASC', 'show_last_update' => 0, 'style' => 'list',
    		'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1,
    		'child_of' => 0, 'feed' => '', 'feed_image' => '', 'exclude' => '',
    		'hierarchical' => true, 'title_li' => __('Categories') ,
    		'echo' =>1); // add echo arg in the $defaults
    
    		.............. and so on........
    
    	if(!$echo)
    		return apply_filters('wp_list_categories', $output);
    
    	echo apply_filters('wp_list_categories', $output);
    
    }
    
    function wp_get_archives($args = '') {
    	global $wp_locale, $wpdb;
    
    	$rt =''; // Add this variable to store the stuff..	
    
    	if ( is_array($args) )
    		$r = &$args;
    	else
    		parse_str($args, $r);
    
    	$defaults = array('type' => 'monthly', 'limit' => '', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1);
    
    ........
    
    replace all the
    
    echo get_archives_link($url, $text, $format, $before, $after);
    
    by 
    
    $rt .= get_archives_link($url, $text, $format, $before, $after);
    
    .......
    
    and at the end
    .....
    
    	if(!$echo)
    		return $rt;
    	echo $rt;
    }
    
    function wp_register( $before = '<li>', $after = '</li>', $echo=1 ) {
    
    	if ( ! is_user_logged_in() ) {
    		if ( get_option('users_can_register') )
    			$link = $before . '<a href="' . get_option('siteurl') . '/wp-login.php?action=register">' . __('Register') . '</a>' . $after;
    		else
    			$link = '';
    	} else {
    		$link = $before . '<a href="' . get_option('siteurl') . '/wp-admin/">' . __('Site Admin') . '</a>' . $after;
    	}
    
    	if(!echo)
    		return apply_filters('register', $link);
    
    	echo apply_filters('register', $link);
    }
    
    function wp_loginout($echo=1) {
    	if ( ! is_user_logged_in() )
    		$link = '<a href="' . get_option('siteurl') . '/wp-login.php">' . __('Login') . '</a>';
    	else
    		$link = '<a href="' . get_option('siteurl') . '/wp-login.php?action=logout">' . __('Logout') . '</a>';
    	if(!$echo)
    		return  apply_filters('loginout', $link);
    	echo apply_filters('loginout', $link);
    }
    
    function wp_register( $before = '<li>', $after = '</li>', $echo=1 ) {
    
    	if ( ! is_user_logged_in() ) {
    		if ( get_option('users_can_register') )
    			$link = $before . '<a href="' . get_option('siteurl') . '/wp-login.php?action=register">' . __('Register') . '</a>' . $after;
    		else
    			$link = '';
    	} else {
    		$link = $before . '<a href="' . get_option('siteurl') . '/wp-admin/">' . __('Site Admin') . '</a>' . $after;
    	}
    
    	if(!$echo)
    		return apply_filters('register', $link);
    
    	echo apply_filters('register', $link);
    }

    sorry if it's a bit random, I made those modifications back when wordpress 2.1 first came out. I kinda just copy pasted the source here and cut out the unimportant parts...

    It would be really great if it was included in future versions so that i won't have to modify the core files
    by myself each time.

    Posted: 7 years ago #
  6. jayp
    Member

    12345

    Here, here. What could be more clear cut than giving designers and developers the option of how they want the data returned? And that some functions will return an array but others won't -- utterly confusing. And it's even more disheartening that this issue doesn't even have that good of a vote...

    I daily become more and more discouraged by WordPress and it's idiosyncrasies.

    Posted: 6 years ago #
  7. adamrice
    Member

    12345

    Agreed. Having just created my own theme, I was frequently confounded by WP functions that behaved almost-but-not-quite the way I wanted, and wound up hacking my own versions of them. This shouldn't be necessary.

    Posted: 6 years ago #
  8. Chip Bennett
    Theme Review Admin

    This is already implemented.

    Refer to the Codex for wp_list_pages().

    This tag returns a list, of pages, either wrapped in UL tags (with a header) or not, depending on the parameters used.

    Also, the results are echoed by default, but can be simply returned (e.g. for further use in a php function), by changing the echo parameter. If echo=false is used, then the results are returned as a string.

    Posted: 4 years ago #
  9. mario oyunu
    Member

    Sometimes I'd like to be able to combine the results of a few different template functions.

    Posted: 4 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    12345
    81 Votes
  • Status

    This idea has been implemented