• Hi this is my first plugin, im trying to show a message if a checkbox is activated and saved, but is not working i need help, this is my code:

    <?php
    /*
    Plugin Name: My Plugin
    Description: My plugin
    Author: My Plugin
    Version: 1.0
    */
    
    add_action( 'admin_menu', 'test_plugin_setup_menu', 'test_init2' );
    
    function test_plugin_setup_menu() {
    		add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', 'test_init', 'test_init2' );
    
    }
    
    function test_init() {  ?>
      <div class="wrap">
    		<h1>Plugin</h1>
    		<form method="post" action="options.php">
      		<input type="checkbox" name="test_init" value="1" <?php checked( 1, get_option( 'test_init2' ), true ); ?> />
    			<?php submit_button(); ?>
    		</form>
    	</div>
    	<?php
    }
    
    function test_init2() {
    		echo '<h1>Message</h1>';
    }
    
    function register_option_in_db() {
    add_option( 'test_init2', $_POST['test_init'], '', 'yes' );
    }
    
    add_action( 'admin_init', 'register_option_in_db' );
    
    ?>

    thanks in advance.

    • This topic was modified 5 years, 10 months ago by Jan Dembowski.
Viewing 6 replies - 1 through 6 (of 6 total)
  • To get you started, I’ve included a quick revision of your code, simplified, standardized, and with recommendations for security. There are some commented notes in the code for you to consider as you move forward.

    <?php
    /*
    Plugin Name: My Plugin
    Description: My plugin
    Author: gigasize777 + Little Package
    Version: 1.0
    */
    defined( 'ABSPATH' ) || exit;
            
    if ( ! class_exists( 'MP_Test_Plugin' ) ) :
    
        class MP_Test_Plugin {
    
            public function __construct() {
              
                add_action( 'admin_menu', array( $this, 'setup_menu' ) );
                add_action( 'admin_init', array( $this, 'register_options' ) );
    
            }
            
            public function setup_menu() {
                 // add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null )
                    add_menu_page( 'Test Plugin Page', 'Test Plugin', 'manage_options', 'test-plugin', array( $this, 'test_menu_page' ), 'dashicons-smiley' );
            }
    
            public function test_menu_page() { 
            
                // you'll definitely want to check user privs and nonces here
                if ( isset( $_POST['option_page'] ) && $_POST['option_page'] == 'mp_settings_group' ) {
                    if ( isset( $_POST['test_init'] ) ) {
                        update_option( 'test_init', 'yes' );
                    } else {
                        update_option( 'test_init', 'no' );
                    }
                }
                
                $test_option = get_option( 'test_init', 'no' );
                ?>
                <div class="wrap">
                    <h1>Plugin</h1>
                    <!-- should NONCE this form -->
                    <form method="post"> <!-- action="options.php" will redirect you to options page on submit -->
                        <?php settings_fields( 'mp_settings_group' ); ?>
                        <?php do_settings_sections( 'mp_settings_group' ); ?>
                        <input type="checkbox" name="test_init" value="<?php echo $test_option; ?>" <?php checked( $test_option, 'yes' ); ?> id="test_init" /><label for="test_init"> We're good?</label>
                            <?php submit_button(); ?>
                    </form>
                </div>
    
                <?php
                
                if ( $test_option == 'yes' ) { 
                    echo 'We\'re good.'; 
                } else {
                    echo 'Don\'t leave me hanging';
                }
                
            }
    
            public function register_options() {
                register_setting( 'mp_settings_group', 'test_init' ); // settings group for if you want to register group styling with WP later
            } 
        }
        
    endif;
    
    if ( is_admin() ) { // let's only run this plugin on the WP backend
        new MP_Test_Plugin();
    }
    Thread Starter gigasize777

    (@gigasize777)

    thank you very much Little Package for your help, now i trying to change the style of the login page of wordpress i have added this code but doesn’t work:

         if ( $test_option == 'yes' ) { 
                    echo 'We\'re good.'; 
                    
     wp_enqueue_style( 'custom-login', plugins_url("/css/style-login.css", __FILE__ ));
                        add_action( 'login_enqueue_scripts', 'my_login_stylesheet' );
    
                    
                } else {
                    echo 'Don\'t leave me hanging';
                }
                
            }
    

    @gigasize777

    Sorry, your last post proves to me you need to hire professional help at this point. Check out: https://jobs.wordpress.net/post-a-job/

    This forum is not the place to get custom code built for you for free. Sorry to be blunt, but it seems like you have not at all read what myself and another poster (on your duplicate post) have already told you.

    Good luck on your project!

    Thread Starter gigasize777

    (@gigasize777)

    thank you again Dear Little Package for you response, I’m really sorry for having done a double post but I did it because another user told me that I should pay a large amount of money to teach me how to make practically a “hello world” application in WordPress, I’m from Venezuela and the highest salary for a professional here is $10/month, here people fight every day to survive, I know that all this is not your problem, but I want you to know that if I had the money I would pay it to you, I thought at the beginning that as WordPress was free software and support was too, I didn’t know that the WordPress forum was paid for simple questions like a “hello world” and non complete applications, I just want to learn that is my motivation.

    Keep studying @gigasize777 — and best of luck to you!

    Moderator bcworkz

    (@bcworkz)

    @gigasize777 – You wrote

    I didn’t know that the WordPress forum was paid for simple questions…

    The WordPress Forums most certainly are not for pay. Soliciting payment in these forums is against our guidelines. While we cannot control what happens outside these forums, we strongly urge members to never engage with anyone soliciting payment in exchange for assistance as a result of any topic in these forums. If you want to hire someone, only interact with those where you initiated contact.

    These forums are operated entirely by volunteers. No one is paid to help here, everyone has their own reasons to volunteer without compensation. As such, each member decides for themselves how much assistance they’ll provide. Some will do much more coding than others to help out.

    Keep on learning for yourself. If you get stuck on something by all means seek the free assistance here. Bear in mind though that targeted, specific issues are more likely to be answered here. Wide ranging, open ended topics and asking for significant, complete coding solutions are less likely to be answered.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Check box code problem’ is closed to new replies.