1

I'm new to php and wp plugin development, i just created a simple options page for my test plugin, the options page has only one check box, i don't know how to execute some function when the box is checked,

My plugin options page is like this

<?php

add_action( 'admin_menu', 'chec_add_admin_menu' );
add_action( 'admin_init', 'chec_settings_init' );


function chec_add_admin_menu(  ) { 

add_options_page( 'Checking', 'Checking', 'manage_options', 'checking', 'checking_options_page' );

}


function chec_settings_init(  ) { 

register_setting( 'my_option', 'chec_settings' );

add_settings_section(
    'chec_checking_section', 
    __( 'Your section description', 'wp' ), 
    'chec_settings_section_callback', 
    'checking'
);

add_settings_field( 
    'chec_checkbox_field_0', 
    __( 'Settings field description', 'wp' ), 
    'chec_checkbox_field_0_render', 
    'checking', 
    'chec_checking_section' 
);


}


function chec_checkbox_field_0_render(  ) { 

$options = get_option( 'chec_settings' );
?>
<input type='checkbox' name='chec_settings[chec_checkbox_field_0]' <?php checked( $options['chec_checkbox_field_0'], 1 ); ?> value='1'>
<?php

}

function chec_settings_section_callback(  ) { 

echo __( 'This section description', 'wp' );

}


function checking_options_page(  ) { 

?>
<form action='options.php' method='post'>

    <h2>Checking</h2>

    <?php
    settings_fields( 'my_option' );
    do_settings_sections( 'checking' );
    submit_button();
    ?>

</form>
<?php

}

?>

Now i want to remove wp logo in admin bar when the check box is checked using the following code

add_action( 'admin_bar_menu', 'remove_wp_logo', 999 );
function remove_wp_logo( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'wp-logo' );
}

How to do this?

0

You would use a conditional check to determine if the option is selected, before adding your function to the action hook.

Example:

$options = get_option( 'chec_settings' );
if ( $options['chec_checkbox_field_0'] == '1' ) {
    add_action ('admin_bar_menu', 'remove_wp_logo', 999 );
}
| improve this answer | |
  • Thanks edwardr for your response, but code you have given is not working, instead i'm getting Parse error: syntax error, unexpected '==' – GKK Dec 16 '14 at 17:24
  • Ugh, so sorry, had a parenthesis in the wrong spot. Try the edited code. – edwardr Dec 16 '14 at 18:38
  • if the check box is checked then wp logo is removed for all user roles, how to execute the function for specific user role if the check box is checked? – GKK Dec 17 '14 at 13:33
  • See current_user_can for a capability conditional. – edwardr Dec 17 '14 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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