1

I created an input field in general - settings. It's in a plugin for a for a local ngo network site.

It looks like this, and it works great.

$ngob_sitelist_slug = new ngob_sitelist_slug();

class ngob_sitelist_slug {
    function ngob_sitelist_slug( ) {
        add_filter( 'admin_init' , array( &$this , 'ngob_register_slug' ) );
    }
    function ngob_register_slug() {
        register_setting( 'general', 'sitelist_slug', 'esc_attr' );
        add_settings_field('sitelist_slug', '<label for="sitelist_slug">'.__('Slug för site-lista' , 'ngo-branding' ).'</label>' , array(&$this, 'ngob_slug_html') , 'general' );
    }
    function ngob_slug_html() {
        $value = get_option( 'sitelist_slug', '' );
        echo '<input type="text" id="sitelist_slug" name="sitelist_slug" value="' . $value . '" />';
    }
}

However I only want it to be created for the network (main) site on a WPMU installation.

So I did this;

    // Get site id
$blog_id = get_current_blog_id();

// Check if we are on network site
if( is_main_site( $blog_id ) ) {

    $ngob_sitelist_slug = new ngob_sitelist_slug();

    class ngob_sitelist_slug {
        function ngob_sitelist_slug( ) {
            add_filter( 'admin_init' , array( &$this , 'ngob_register_slug' ) );
        }
        function ngob_register_slug() {
            register_setting( 'general', 'sitelist_slug', 'esc_attr' );
            add_settings_field('sitelist_slug', '<label for="sitelist_slug">'.__('Slug för site-lista' , 'ngo-branding' ).'</label>' , array(&$this, 'ngob_slug_html') , 'general' );
        }
        function ngob_slug_html() {
            $value = get_option( 'sitelist_slug', '' );
            echo '<input type="text" id="sitelist_slug" name="sitelist_slug" value="' . $value . '" />';
        }
    }
}

However, I get this error when reloading the page:

Fatal error: Uncaught Error: Class 'ngob_sitelist_slug' not found...(shortened the output, since it's mostly path:s)

Why is that? Im fairly novis on classes to start with, but when not on network site, the class doesn't get executed, so no problem, as expected. But when on network site, I get above error..

How to mend this so I can get that input field, but only on the main (network) site?

I don't fully understand what the problem is, and it seems hard to find anything on google about this. I guess you can't wrap a class in an if? But why not, and mostly, how to solve the issue?

2 Answers 2

2

How are you invoking ngob_sitelist_slug?

I'm gonna guess that you are doing something like $my_class = new ngob_sitelist_slug;. Doing it this way would require that your class have a constructor. if it doesn't, you'll get an error.

You could still call your class methods statically:

$my_thing = ngob_sitelist_slug::my_method();

Though technically you should add the static keyword to your methods in this case.

Also I see that in your case you are using $this, so you really need a constructor.

EDIT: I now realize you do have a constructor - albeit a deprecated one.

1
  • Yeah, I guess I have more to learn about classes. Read a bit in php docs, and mostly used examples I could find. You know of somewhere to read it more condensed with examples? Commented Sep 29, 2016 at 18:28
0

I solved the problem by doing this;

class ngob_sitelist_slug {
    function ngob_sitelist_slug( ) {
        // Check if we are on network site
        if( is_main_site( $blog_id ) ) {
            add_filter( 'admin_init' , array( &$this , 'ngob_register_slug' ) );
        }
    }

The rest of the code is the same. But I guess there has to be a better aproach? How should you do it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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