• Resolved abufoysal

    (@abufoysal)


    I am learning plugin development. I am submitting a Form in admin-post.php from Admin Panel. I would like to show a Admin Notice while a Form field is empty. My admin-post.php file is like below.

        <?php
        
        if ( ! defined( 'WP_ADMIN' ) ) {
        	define( 'WP_ADMIN', true );
        }
        
        if ( defined( 'ABSPATH' ) ) {
        	require_once( ABSPATH . 'wp-load.php' );
        } else {
        	require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
        }
        send_origin_headers();
        
        require_once( ABSPATH . 'wp-admin/includes/admin.php' );
        
        nocache_headers();
        do_action( 'admin_init' );
        
        $action = empty( $_REQUEST['action'] ) ? '' : $_REQUEST['action'];
        
        if ( ! is_user_logged_in() ) {
        	if ( empty( $action ) ) {
        		
        		do_action( 'admin_post_nopriv' );
        	} else {
        		
        		do_action( "admin_post_nopriv_{$action}" );
        	}
        } else {
        
        		add_action( 'admin_post_addAddress', 'add_new_address' );
        
        		function my_acf_notice() {
        			?>
        			<div class="update-nag notice">
        					<p><?php _e( 'Name field is empty!', 'my_plugin_textdomain' ); ?></p>
        			</div>
        			<?php
        		}
        
        		function add_new_address(){
        		if($_POST['name'] == '') {
        			add_action( 'admin_notices', 'my_acf_notice' );  // This line is not working.			
        		}
        
        		$retrieved_nonce = $_REQUEST['_wpnonce'];
        		if (wp_verify_nonce($retrieved_nonce, 'add_new_address' ) ) {
        			if(isset( $_POST['add_new_address'] )) {
        				global $wpdb;
        				$sql = $wpdb->prepare(
        				"INSERT INTO '".$wpdb->prefix."afaddresss'('name','email','phone_no','address','photo') values ( %s, %s, %s,%s, %s ) ", array($_POST['name'], $_POST['email'], $_POST['phone_no'], $_POST['address'], $_POST['photo']));
        				$result = $wpdb->query($sql);
        				if($result) {
        					wp_redirect( admin_url( '/admin.php?page=addressPage' ) );
        				}
        			}
        		}
        	}
        
        	if ( empty( $action ) ) {
        		do_action( 'admin_post' );
        	} else {
        		do_action( "admin_post_{$action}" );
        	}
        }

    But this line add_action( 'admin_notices', 'my_acf_notice' ); is not working.

    • This topic was modified 5 years ago by bcworkz. Reason: code fixed
Viewing 5 replies - 16 through 20 (of 20 total)
  • Thread Starter abufoysal

    (@abufoysal)

    Thanks @bcworkz for your reply.

    Here is link of Full WordPress. You can download it and install in your own machine.

    https://drive.google.com/open?id=1cWCX5nK3l8yWJv9yyHo88JR3a7u3owlu

    Thanks

    Moderator bcworkz

    (@bcworkz)

    Thank you, but I have already installed WP as a local test site, downloaded from the official .org download site. In any case I’m not going to install software from an unauthorized source.

    Thread Starter abufoysal

    (@abufoysal)

    Thanks @bcworkz . You don’t have to install anything. You can copy my Plugin code to your installed WordPress. Thanks.

    Moderator bcworkz

    (@bcworkz)

    OK, you could have just packaged your plugin files ??

    Your form has a hidden “action” field that’s taking precedence of over “my_action”. Rename the field or comment it out and the Hello World! output will appear on form submit.

    I know your plugin is still in development and perhaps you intend to go through and modify URLs in code. In any case, don’t hardcode full URLs. Use WP functions or constants. For example in function addressPage() you have:
    <a href="https://127.0.0.1/wordpress1/wp-admin/post-new.php?post_type=page" class="page-title-action">Add New</a>';
    That obviously wouldn’t work on many other sites. Even default folders in WP can be redefined. For portability use WP functions to get the site’s proper URL. For example:
    <a href="<?php echo admin_url('post-new.php?post_type=page'); ?>" class="page-title-action">Add New</a>';

    Thread Starter abufoysal

    (@abufoysal)

    Thanks @bcworkz your solution is working. Thanks.

Viewing 5 replies - 16 through 20 (of 20 total)
  • The topic ‘Use admin_notices Action hook’ is closed to new replies.