WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to override wp_list_pages and wp_list_bookmarks without modify core code ? (3 posts)

  1. Franky57
    Member
    Posted 11 months ago #

    Hi all !

    I am finding a way to override some core functions of WordPress (wp_list_pages and wp_list_bookmarks) within my function.php theme file because I am so tired to always modify core code when I update WordPress.

    In the way to make easier my css styling, I proceed of this changes in post-template.php and bookmark-template.php core file.

    In one hand, I added in the $default array of the post-template.php wp_list_pages function the following parameters:

    'before' => '', 'after' => ''

    in the way to modify the Walker_Page start_el method at line 1062 from:

    $output .= $indent . '<li class="' . $css_class . '"><a href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>';

    to:

    $output .= $indent . '<li class="' . $css_class . '">' . $before . '<a href="' . get_permalink($page->ID) . '">' . $link_before . apply_filters( 'the_title', $page->post_title, $page->ID ) . $link_after . '</a>' . $after;

    (as we can see these parameters in the wp_list_bookmarks function)

    In the other hand, I added in the $default array of the bookmark-template.php _walk_bookmarks and wp_list_bookmarks functions the following parameter:

    'relativepath' => 0

    in the way to modify the part of code at line 102 in _walk_bookmarks function from:

    if ( $bookmark->link_image != null && $show_images ) {
    	if ( strpos($bookmark->link_image, 'http') === 0 )
    		$output .= "<img src=\"$bookmark->link_image\" $alt $title />";
    	else // If it's a relative path
    		$output .= "<img src=\"" . get_option('siteurl') . "$bookmark->link_image\" $alt $title />";
    
    	if ( $show_name )
    		$output .= " $name";
    	} else {
    		$output .= $name;
    }

    to:

    if ( $bookmark->link_image != null && $show_images ) {
    	if ( $relativepath == 0 ) {
    		if ( strpos($bookmark->link_image, 'http') === 0 )
    			$output .= "<img src=\"$bookmark->link_image\" $alt $title />";
    		else // If it's a relative path
    			$output .= "<img src=\"" . get_option('siteurl') . "$bookmark->link_image\" $alt $title />";
    	} else {
    		$output .= "<img src=\"" . wp_make_link_relative($bookmark->link_image) . "\" $alt $title />";
    	}
    
    	if ( $show_name )
    		$output .= " $name";
    } else {
    	$output .= $name;
    }

    (I need to have relative path for the src attribute of my img tag and I don't find on other way even with hooks and filters)

    I know it's dangerous to modify core code but I don't find on internet the way to obtain what I needed without hacking WordPress core files.

    Is anybody have an idea to get around my hacking code ?

    Thanks

  2. bcworkz
    Member
    Posted 11 months ago #

    You would need to work off the filters of the same name of each function to search and replace the output string to achieve any modifications. I know it seems like a crude and fragile way to do things, but that is what is available to us to work cleanly. You would still need to check that your search parameters work after upgrades, but normally nothing would need to be changed.

    The only other alternative is to develop your own template tags and call them instead. You could then extend your own walker class and define the start_el method as you require. Since it would be mostly a copy and rename exercise, this could be a viable approach. By changing template tag calls on a child theme template, you need never re-edit any code again, though it would be a good idea to stay on top of any changes to the core functions and classes you copied and renamed. While perhaps not truly "clean", it does save you from the need to re-edit.

  3. Franky57
    Member
    Posted 11 months ago #

    Thanks for your suggestions.

    I will try to look again with hooks and filters way, but I don't find anything to solve my troubles at the time.

    Maybe extend my own walker class could solve a part of my needs.

    I will look for a solution again.

    Thanks.

Reply

You must log in to post.

About this Topic