Support » Fixing WordPress » How to change the body color in my plug-in option page?

  • I have followed the example shown in the link from @lucspe but it is not working for me, I’m not sure if this is up to date, cause I cant get it to work, here is what I have on my plugin class:

    public function __construct() {
    	add_action( 'admin_head', 'wpso_add_admin_custom_css' );
    	add_action( 'admin_menu', array( $this, 'settings_add_plugin_page' ) );
    	add_action( 'admin_init', array( $this, 'settings_page_init' ) );		
    }
    
    function wpso_add_admin_custom_css() {
      ?>
        <style type="text/css">
            #wpadminbar {
                background-color : #f1f1f1;
            }       
            #wpcontent {background-color: #f0f0f0; }
        </style>
      <?php
    }

    I feel like the function is not being run, this is my class creation:

    public function __construct() {
            add_action( 'admin_head', 'wpso_add_admin_custom_css' );
            add_action( 'admin_menu', array( $this, 'settings_add_plugin_page' ) );
            add_action( 'admin_init', array( $this, 'settings_page_init' ) );       
    }

    This is where I create the class:

    add_action('plugins_loaded', 'stwc_loaded');
    function stwc_loaded(){    
        if ( is_admin() )
        $settings = new Settings();    
    }
    • This topic was modified 1 year, 4 months ago by Syncly.it. Reason: typo
    • This topic was modified 1 year, 4 months ago by Syncly.it.

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    “plugins_loaded” fires awfully early. is_admin() might not yet be functional? Why not use a later, admin specific hook like “admin_init”?

    Thread Starter Syncly.it

    (@elnath78)

    @bcworkz

    if you refer to class creation:

    add_action('plugins_loaded', 'stwc_loaded');
    function stwc_loaded(){    
        if ( is_admin() )
        $settings = new Settings();    
    }

    This is firing correctly, what I dont get to fire is the function:

    function wpso_add_admin_custom_css() {
      ?>
        <style type="text/css">
            #wpadminbar {
                background-color : #f1f1f1;
            }       
            #wpcontent {background-color: #f0f0f0; }
        </style>
      <?php
    }

    I also tried with do_action() but nothing, there is no way to call this function. Another test was using echo to print the style, absolutely nothing. I finally tried printing a log file but it never enter this routing, no matter how loud I cry or my cat comes visiting me on the desk.

    Moderator bcworkz

    (@bcworkz)

    I don’t think if ( is_admin() ) is working like you’d expect because is_admin() is called too early. Try commenting it out to see if you style block gets output. Needlessly adding 3 hooks for non-admin requests wouldn’t be the end of the world, but if you want is_admin() to work correctly, use it in a later hook, like “init”. I think it still fires before “admin_init”.

    Thread Starter Syncly.it

    (@elnath78)

    @bcworkz (do I need to mention you or you get notifications regardless??)

    I think that we are talking of two different thing, to clarify this up, I’m using is_admin only in one place, here:

    add_action('plugins_loaded', 'stwc_loaded');
    function stwc_loaded(){    
        if ( is_admin() )
        $settings = new Settings();    
    }

    To check if the current user can instantiate the Settings class. I’m saying that this is working cause otherwise I’d not have any functionality in my plug-in, included no menu, no page creation, nothing.

    inside my plass then I have this:

    public function __construct() {		
    		//add_action( 'admin_head', 'wpso_add_admin_custom_css', 10, 2 );		
    		add_action( 'admin_menu', array( $this, 'settings_add_plugin_page' ) );
    		add_action( 'admin_init', array( $this, 'settings_page_init' ) );		
    	}

    That if I read you right, is bein executed during the ‘plugins_loaded’ where is_admin is not yet working and therefore able to define the admin_head.

    I then moved the is_admin insdie the admin_init no difference, I also tryed commenting it out entirely, and still no difference, no way to trigger admin_head I don’t know what else to think.

    Moderator bcworkz

    (@bcworkz)

    People get notifications of new replies only if they are subscribed to the topic, or if they are @ mentioned. If someone chooses to not subscribe, it could be seen as a little rude to force a notification via @ mention anyway. IMO there is little need to ever @ mention anyone here, but it’s a common usage in other forums. Use it if you like for participants in the topic, but avoid doing so for non-participants. You shouldn’t have @ mentioned lucspe in your OP since they are not a participant. Not a bog deal though. I let it slide initially, but since you’ve brought up @ mentions, they are optional, but should be limited to topic participants when used.

    If you’re seeing other class functionality, then I’m wrong about is_admin() being called too early. Because plugins load very early, I’m always leery of using most WP functions beyond add_action() or add_filter() that soon. I’m unsure what else could be the problem.

    Are you sure the style block isn’t appearing at all in the page’s source HTML, or are you assuming so since the styles are not applied? Application could fail despite the style block being there if there’s an issue with the CSS.

    wpso_add_admin_custom_css() is a procedural function that is outside of your class, correct? Otherwise you’ve added it incorrectly if it’s a class method.

    In your last snippet the add_action() call for this is commented out, so of course there will be no style block output.

    Define WP_DEBUG as true in wp-config.php to be notified of any PHP errors. There could be some minor syntax error that we’re not seeing. You can confirm beyond doubt if some section of code executes or not by inserting error_log() calls at strategic locations, then checking the log file to confirm.

    Thread Starter Syncly.it

    (@elnath78)

    @bcworkz,

    I was calling it inside the class, but also tried to move it outside:

    if ( is_admin() )
    add_action( 'admin_head', 'wpso_add_admin_custom_css', 10, 2 );     
    $settings = new stwc_settings();
    
    

    function wpso_add_admin_custom_css() {
    $log = “2 – ” . $text;
    file_put_contents(STWC_PATH . ‘./log_’.date(“j.n.Y”).’.txt’, $log, FILE_APPEND);

    ?>
    <!– debug –>
    <style type=”text/css”>
    #wpadminbar {
    background-color : #f1f1f1;
    }
    #wpcontent {background-color: #f0f0f0; }
    </style>
    <?php
    }`

    I say it is not firing both for the missing background on body and cause the log is not written, I add the callback function but never fires.

    <blockquote>wpso_add_admin_custom_css() is a procedural function that is outside of your class, correct?</blockquote>

    Im not sure what you mean, you can have callbacks on class functions as well, example:

    public function __construct() {			
    		add_action( 'admin_menu', array( $this, 'settings_add_plugin_page' ) );
    		add_action( 'admin_init', array( $this, 'settings_page_init' ) );		
    	}

    The callback in this case is inside the class, the functions are public indeed.

    • This reply was modified 1 year, 4 months ago by Syncly.it.
    Moderator bcworkz

    (@bcworkz)

    Either class or procedural (outside of any class) functions can be added, but you add differently depending on what kind of function. As public class method, added in the same class:
    add_action( 'admin_menu', array( $this, 'wpso_add_admin_custom_css' ) );

    As procedural:
    add_action( 'admin_menu', 'wpso_add_admin_custom_css' );

    Out of curiosity, I put this on my test site and it works as expected:

    add_action('plugins_loaded', 'stwc_loaded');
    function stwc_loaded(){    
        if ( is_admin() )
        $settings = new Settings();    
    }
    class Settings {
    	public function __construct() {
    		add_action( 'admin_head', [$this, 'wpso_add_admin_custom_css'] );
    	}
    
    	function wpso_add_admin_custom_css() {
    	  ?>
    		<style type="text/css">
    		    #wpadminbar {
    		        background-color : #31f1f1;
    		    }       
    		    #wpcontent {background-color: #30f0f0; }
    		</style>
    	  <?php
    	}
    }

    I changed the colors to cyan to make it obvious if the styles were applied. They were, but I cannot recommend the resulting color scheme šŸ™‚

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to change the body color in my plug-in option page?’ is closed to new replies.