need to modify core file object method WP_Widget_Pages->widget
-
hello wordpress users ??
i need to modify a WP core function in /wp-includes/default-widgets.php
there i find the object WP_Widget_Pages and i need to modify the class method function widget()
i was looking for some action hooks in the function in the hope that i could create a hook … but couldn’t find a do_action call …
so … what would be the best method to modify this function as i don’t always want to hack into the WP core files when doing an upgrade.
any hint or help would be more than welcome ??
thanks & all the best
becki
-
What are you actually trying to achieve and why? If we know we can maybe help you, or at least point you in the right direction.
hello duck__boy,
thanks for your reply … i will copy the original and modified code below so you can have a look what i’m trying to achive ->
original code first ->
function widget( $args, $instance ) { extract( $args ); $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base); $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby']; $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude']; if ( $sortby == 'menu_order' ) $sortby = 'menu_order, post_title'; $out = wp_list_pages( apply_filters('widget_pages_args', array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) ) ); if ( !empty( $out ) ) { echo $before_widget; if ( $title) echo $before_title . $title . $after_title; ?> <ul> <?php echo $out; ?> </ul> <?php echo $after_widget; } }
modified function follows ->
function widget( $args, $instance ) { extract( $args ); $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base); $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby']; $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude']; if ( $sortby == 'menu_order' ) $sortby = 'menu_order, post_title'; $out = wp_list_pages( apply_filters('widget_pages_args', array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) ) ); if ( !empty( $out ) ) { // echo $before_widget; core hack if ( $title) // echo $before_title . $title . $after_title; core hack ?> <ul class="vlist"> <?php echo $out; ?> </ul> <?php // echo $after_widget; core hack } }
so … what am i modifying?
first i need to remove the $before & $after_widget
then i need to remove the $title from the widget …
last … i need to add a CSS class named ‘vlist’ to the
‘ul’ container
this is a request from my customer … he doesn’t want to have the widget title ‘pages’what’s the best way to achieve this without modifying the core files?
thanks & fun
beckiThose values are speciied per sidebar and can be set differently for each sidebar –
$args = array( 'name' => sprintf(__('Sidebar %d'), $i ), 'id' => 'sidebar-$i', 'description' => '', 'before_widget' => '<li id="%1$s" class="widget %2$s">', 'after_widget' => '</li>', 'before_title' => '<h2 class="widgettitle">', 'after_title' => '</h2>' ); ?> register_sidebar( $args );
You best and easiest bet would be to create sidebars using the above code in the ‘functions.php’ file. That way, they can be updated dynamically and you will never need to re-hack.
Look here for further information – Function Reference/register sidebar
yep …thanks a lot for your idea ??
the register_sidebar function would take care of the ->
before_widget & after_widget
before_title & after_titlegreat!
but this would still lack 2 things I’m trying to achieve …
first … completely remove the $title!
and add a CSS class ‘vlist’ to the UL container!how can this be done without modifying the core file??
any more input, ideas & hints are more than welcome ??
greetings
beckiYou can also make your own custom widgets that will achieve everything you want. A live example that is used on my companies website (https://www.dynedrewett.com) is below.
Could you also achieve what you want through looking at the scource and amending a class that is already used in the ‘UL’ container? This would perhaps save rewriting all of the widgets.
Recommended reading is Multi-widgets
The code for the default widgets is in ‘wp_includes\default-widgets.php’ – you could copy any you want in to a ‘custom-widgets.php’ file and amend as necessary.
/** * Declare your custom multi-widget */ class Custom_Widget_Brochures extends WP_Widget { /** constructor */ function Custom_Widget_Brochures() { $widget_options = array('classname' => 'custom_widget_brochures', 'description' => __('Includes a list of available Brocures in the sidebar.')); parent::WP_Widget(false, $name = 'Brochures', $widget_options); } /** @see WP_Widget::widget */ function widget($args, $instance) { extract($args); $title = apply_filters('widget_title', $instance['title']); if(!$title || $title === '' || $title === null) $title = 'View our brochures'; ?> <?php echo $before_widget; ?> <?php $brochures = get_posts('numberposts=10&post_type='.POST_TYPE_BROCHURE.'&orderby=name&order=ASC'); if(!empty($brochures)){ ?> <div id="sidebar-brochures" class="rightColItem rc_title_bar rc_title"> <?php echo $title; ?> </div> <div class="rightColItem pinkLinks"> <ul class="rc_linkList"> <?php foreach($brochures as $brochure){ ?> <li class="link_arrow"><a href="<?php echo get_permalink($brochure->ID); ?>"><?php echo $brochure->post_title; ?></a></li> <?php } ?> </ul> </div> <?php } ?> <?php echo $after_widget; ?> <?php } /** @see WP_Widget::update */ function update($new_instance, $old_instance){ $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); return $instance; } /** @see WP_Widget::form */ function form($instance){ $title = esc_attr($instance['title']); ?> <p> <label for="<?php echo $this->get_field_id('title'); ?>"> <?php _e('<strong>Title:</strong>'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /> </label> </p> <?php } }
/** * Register all of your custom WordPress widgets on startup */ add_action('widgets_init', 'custom_widgets_init'); function custom_widgets_init() { register_widget('Custom_Widget_Brochures'); }
/** * Remove unwanted default sidebar widgets */ add_action('admin_head', 'edit_registered_widgets'); function edit_registered_widgets(){ global $wp_registered_widgets; unset($wp_registered_widgets['pages-1']); unset($wp_registered_widgets['calendar-1']); unset($wp_registered_widgets['archives-1']); unset($wp_registered_widgets['links-1']); unset($wp_registered_widgets['meta-1']); unset($wp_registered_widgets['search-1']); unset($wp_registered_widgets['text-1']); unset($wp_registered_widgets['categories-1']); unset($wp_registered_widgets['recent-posts-1']); unset($wp_registered_widgets['recent-comments-1']); unset($wp_registered_widgets['rss-1']); unset($wp_registered_widgets['tag_cloud-1']); unset($wp_registered_widgets['nav_menu-1']); }
Hope that helps.
hey duck__boy ??
you’re the man! custom widgets saved my day ??
did create my custom widget class extending from WP_Widget and saved the code in the themes function.php file
then registered my custom widget ->
register_widget(‘myCustomWidget’);and voila … in the admin panel the widget shows up and i can drag & drop it into the sidebar …
no more modifying of WP core files … problem solved ??
thanks again for pointing me towards the right direction!
greetz
beckiYou’re welcome. I was also very pleased when I discovered how to do this, as it really is a powerful weapon in WP’s arsenal!
- The topic ‘need to modify core file object method WP_Widget_Pages->widget’ is closed to new replies.