WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Hi I am writing a plugin and would like to extend a class in the wp-includes folder. I mean that literally like

class my_class extends wp_includes_class_x{ ... }

However when attempting this I get a class x not found error but I know the class is being used by Wordpress. So I try an include_once(path_to_class) within my plugin file and that gives me a Cannot redeclare class error. Should I be calling the include on a special action hook?

What am I doing wrong?

Thanks guys


My plugin is OO and so I call $my_plugin = new My_Plugin_Class(); in my plugin file. Inside this main class I try to instantiate an instance of the class that extends the wp_includes class that I don't wish to name so that I can keep my project a surprise

$my_plugin = new My_Plugin_Class();

class My_Plugin_Class{

    public function __construct(){
    $wp_include_class_extension = new WP_Class_Extension();


class WP_Class_Extension extends Some_Core_Class{
    public function __construct(){
    //blah blah
//CAUSES ERROR class Some_Core_Class not found


Found a solution - see comments below - thanks for the help guys

share|improve this question
Can you provide a more specific example of what you're trying to do? I don't think wp_includes_class_x is a valid class... – mor7ifer May 9 '12 at 22:22
Thats just an example fake class - I am extending a real class but I don't want to name it until I have finished the project - it is an open source project but I still wish to retain an element of surprise. – JackMahoney May 9 '12 at 22:28
Quite frankly, that obscurity will probably push people away from your question...I'm certainly not gonna try to writeup an answer with zero knowledge of what's going on. Close Voted. – mor7ifer May 9 '12 at 23:22
Dude can you not understand my situation, I am working on a cool new feature to do with beta files in the Wordpress Core and I want it to be a surprise - so posting my work in raw form would reveal to everyone. Its a great feature that will go open source but I wish to be the first to achieve it - is that so bad? – JackMahoney May 9 '12 at 23:38
Thanks for all the help guys - I found a solution. Turns out core was including the file at certain times only, so instead of declaring it in my plugin file I simply moved the extending class to a separate file and called an include on that file when needed - knowing that the parent class would be included at that time. Thanks for the help and I'll release the code open-source soon. Cheers! – JackMahoney May 9 '12 at 23:47
up vote 2 down vote accepted

The Widgets API page on the Codex has a great snippet for extending a class (in this case the WP_Widget class). This should be pretty clear overall. Be sure you're using the standard name of the class and make sure it's an actual class and not just a function.

 * Adds Foo_Widget widget.
class Foo_Widget extends WP_Widget {

     * Register widget with WordPress.
    public function __construct() {
            'foo_widget', // Base ID
            'Foo_Widget', // Name
            array( 'description' => __( 'A Foo Widget', 'text_domain' ), ) // Args

     * Front-end display of widget.
     * @see WP_Widget::widget()
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
    public function widget( $args, $instance ) {
        extract( $args );
        $title = apply_filters( 'widget_title', $instance['title'] );

        echo $before_widget;
        if ( ! empty( $title ) )
            echo $before_title . $title . $after_title;
        ?>Hello, World!<?php
        echo $after_widget;

     * Sanitize widget form values as they are saved.
     * @see WP_Widget::update()
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     * @return array Updated safe values to be saved.
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = strip_tags( $new_instance['title'] );

        return $instance;

     * Back-end widget form.
     * @see WP_Widget::form()
     * @param array $instance Previously saved values from database.
    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        else {
            $title = __( 'New title', 'text_domain' );
        <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
        <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />

} // class Foo_Widget

// register Foo_Widget widget
add_action( 'widgets_init', create_function( '', 'register_widget( "foo_widget" );' ) );
share|improve this answer
Thanks but my knowledge of extending classes in PHP is fine. Say the class name in the include folder is WP_Foo_Helper then my class would look like class My_Class extends WP_Foo_Helper{} because I am assuming the class has been loaded. My problem is that I'm getting no class found errors and then when I include the file containing the class I get a cannot redeclare class error – JackMahoney May 9 '12 at 23:06
Don't take offense, we get all levels here. How are you adding your action (see the last line of code)? – SickHippie May 9 '12 at 23:17
Sorry I wasn't offended just trying to highlight the problem. – JackMahoney May 9 '12 at 23:23
Just making sure - but to answer your question there's really no special way to extend classes in WP. If you were extending the Walker class for a custom nav, you'd use class Walker_Child_Classes extends Walker_page { // code } - same as usual. Without seeing your code or knowing what class you're extending, it's going be really difficult to get help here. – SickHippie May 9 '12 at 23:28
+1 for extensive answer – kaiser May 10 '12 at 0:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.