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?