最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why is my form going to frontend on html form submission present on the php files inside my custom plugin?

programmeradmin5浏览0评论

I'm creating my first Wordpress plugin. It's goal is to update information from admin user about the book stock using sku only.

There are 2 forms in update-stock.php page. First form takes sku input and retrieves stock info. Second form takes the new stock value from the admin user.The page works fine if I submit it on the same page,but i want to simply it.

I don't know how to split and work with php files seamlessly on backend only.

When I tried changing the action attribute to a php file in a sub folder inside my plugin, it works but it does not retain the admin Dashboard but instead takes me to the frontend of the website.

I really want to know how to work with php files in plugin subfolders whilst stay in the backend only.

I have searched a lot online but there seems to be no proper guideline for Wordpress plugin development about how to handles other php files.

Any help or reference will be appreciated.

plugin-name.php

define("PLUGIN_DIR_PATH",plugin_dir_path(__FILE__));
add_action( 'admin_menu', 'wim_register_my_custom_menu_page' );
function wim_register_my_custom_menu_page() {
    add_menu_page(
        __( 'Plugin name', 'textdomain' ),
        'Plugin name',
        'manage_options',
        'plugin-name/plugin-name.php',
        'plugin_name_pagefunc',
        'dashicons-tickets',
        6
    );
    add_submenu_page(
            'plugin-name/plugin-name.php',
            'Update Stock',
            'Update Product',
            'manage_options',
            'update-stock',
            'update_stockfunc',
            'dashicons-welcome-add-page'
        );
}
function plugin_name_pagefunc()
{
 ?>
    <h1>Plugin Name</h1>
    <h3>Thank you for installing our plugin.</h3>
    <p>This plugin is under development. This plugin is being developed for learning plugin development.</p>
<?php
    }    
    function update_stockfunc(){
     include_once PLUGIN_DIR_PATH."/inc/views/update-stock.php";
    }
?>

update-stock.php

<div class="wrap">
        <h2>Update Stock</h2>
        <form method="POST" >
        <input type="number" name="skuInput" onmouseover="this.focus();" maxlength="13">
        <button value="Submit" onclick="Submit" >Check Stock</button>
        </form>
        <hr>
<?php
        $sku = $_POST['skuInput'];
        $product_id = get_product_id_by_sku($sku);
        $stock = get_stock_by_product_id($product_id);
        function get_product_id_by_sku( $sku = false ) {
            global $wpdb;
            if( !$sku )
                return null;
            $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
            if ( $product_id )
                 return $product_id;                
         return null;
        }

        function get_stock_by_product_id( $product_id = false ) {
            global $wpdb;
            if( !$product_id )
                return null;
            $stock = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id='%s' and meta_key='_stock'",$product_id));

             if ( $stock )
                 return $stock;            
         return null;
        }
?>
 <form method="POST" action="<?php echo plugins_url( 'update-stock-success.php', __FILE__ ); ?>">
            <input name="updatedStock" type="number" value ="<?php echo 
            $stock;?>">
            <input type="hidden" name="product_id2" value="<?php echo 
            $product_id ?>"/>
            <button value="Submit" onclick="Submit" >Update Stock</button>
        </form>

update-stock-success.php

<div class="wrap">
    <h2>Product Stock was updated successfully</h2>
        <?php
         $updatedStock = $_POST['updatedStock'];
         $product_id2 = $_POST['product_id2'];
         echo "New updated stock is ".$updatedStock." has post 
         id".$product_id2;
         update_post_meta( $post, '_stock', $updatedStock);
         echo "New updated stock is ".$updatedStock." has post 
         id".$product_id2;
         ?>
</div>

The output goes to frontend as you can see in the image below:

I'm creating my first Wordpress plugin. It's goal is to update information from admin user about the book stock using sku only.

There are 2 forms in update-stock.php page. First form takes sku input and retrieves stock info. Second form takes the new stock value from the admin user.The page works fine if I submit it on the same page,but i want to simply it.

I don't know how to split and work with php files seamlessly on backend only.

When I tried changing the action attribute to a php file in a sub folder inside my plugin, it works but it does not retain the admin Dashboard but instead takes me to the frontend of the website.

I really want to know how to work with php files in plugin subfolders whilst stay in the backend only.

I have searched a lot online but there seems to be no proper guideline for Wordpress plugin development about how to handles other php files.

Any help or reference will be appreciated.

plugin-name.php

define("PLUGIN_DIR_PATH",plugin_dir_path(__FILE__));
add_action( 'admin_menu', 'wim_register_my_custom_menu_page' );
function wim_register_my_custom_menu_page() {
    add_menu_page(
        __( 'Plugin name', 'textdomain' ),
        'Plugin name',
        'manage_options',
        'plugin-name/plugin-name.php',
        'plugin_name_pagefunc',
        'dashicons-tickets',
        6
    );
    add_submenu_page(
            'plugin-name/plugin-name.php',
            'Update Stock',
            'Update Product',
            'manage_options',
            'update-stock',
            'update_stockfunc',
            'dashicons-welcome-add-page'
        );
}
function plugin_name_pagefunc()
{
 ?>
    <h1>Plugin Name</h1>
    <h3>Thank you for installing our plugin.</h3>
    <p>This plugin is under development. This plugin is being developed for learning plugin development.</p>
<?php
    }    
    function update_stockfunc(){
     include_once PLUGIN_DIR_PATH."/inc/views/update-stock.php";
    }
?>

update-stock.php

<div class="wrap">
        <h2>Update Stock</h2>
        <form method="POST" >
        <input type="number" name="skuInput" onmouseover="this.focus();" maxlength="13">
        <button value="Submit" onclick="Submit" >Check Stock</button>
        </form>
        <hr>
<?php
        $sku = $_POST['skuInput'];
        $product_id = get_product_id_by_sku($sku);
        $stock = get_stock_by_product_id($product_id);
        function get_product_id_by_sku( $sku = false ) {
            global $wpdb;
            if( !$sku )
                return null;
            $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
            if ( $product_id )
                 return $product_id;                
         return null;
        }

        function get_stock_by_product_id( $product_id = false ) {
            global $wpdb;
            if( !$product_id )
                return null;
            $stock = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id='%s' and meta_key='_stock'",$product_id));

             if ( $stock )
                 return $stock;            
         return null;
        }
?>
 <form method="POST" action="<?php echo plugins_url( 'update-stock-success.php', __FILE__ ); ?>">
            <input name="updatedStock" type="number" value ="<?php echo 
            $stock;?>">
            <input type="hidden" name="product_id2" value="<?php echo 
            $product_id ?>"/>
            <button value="Submit" onclick="Submit" >Update Stock</button>
        </form>

update-stock-success.php

<div class="wrap">
    <h2>Product Stock was updated successfully</h2>
        <?php
         $updatedStock = $_POST['updatedStock'];
         $product_id2 = $_POST['product_id2'];
         echo "New updated stock is ".$updatedStock." has post 
         id".$product_id2;
         update_post_meta( $post, '_stock', $updatedStock);
         echo "New updated stock is ".$updatedStock." has post 
         id".$product_id2;
         ?>
</div>

The output goes to frontend as you can see in the image below:

Share Improve this question edited Jan 15, 2020 at 9:58 Savio menezes asked Jan 13, 2020 at 4:59 Savio menezesSavio menezes 185 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Add a new URL using add_submenu_page (without any parent assigned) and post your form data on that URL. Refer to this page for adding sub-menu page.

Also, you should not send form data on any PHP file like this. This, will opens up your plugin for external attacks.

Let, know if you unable to figure out the way of doint this.

Have a Good Day!

I updated only those parts which leads to explain the things which I was trying to tell you in my first answer.

plugin-name.php

define("PLUGIN_DIR_PATH",plugin_dir_path(__FILE__));
add_action( 'admin_menu', 'wim_register_my_custom_menu_page' );
function wim_register_my_custom_menu_page() {
    add_menu_page(
        __( 'Plugin name', 'textdomain' ),
        'Plugin name',
        'manage_options',
        'plugin-name/plugin-name.php',
        'plugin_name_pagefunc',
        'dashicons-tickets',
        6
    );

    add_submenu_page(
            '',
            'Update Stock',
            'Update Product',
            'manage_options',
            'update-prod',
            'update_prodfunc',
            'dashicons-welcome-add-page'
        );
}

function plugin_name_pagefunc()
{
    ?>
        <h1>Plugin Name</h1>
        <h3>Thank you for installing our plugin.</h3>
        <p>This plugin is under development. This plugin is being developed for learning plugin development.</p>
    <?php
}   

function update_stockfunc(){
    include_once PLUGIN_DIR_PATH."/inc/views/update-stock.php";
}

function update_prodfunc(){
    echo '<h2>Product Stock was updated successfully</h2>';
    $updatedStock = $_POST['updatedStock'];
    $product_id2 = $_POST['product_id2'];
    echo "New updated stock is ".$updatedStock." has post 
    id".$product_id2;
    update_post_meta( $post, '_stock', $updatedStock);
    echo "New updated stock is ".$updatedStock." has post 
    id".$product_id2;
}

update-stock.php

<form method="POST" action="<?= admin_url() . 'admin.php?page=update-prod' ?>">
    <input name="updatedStock" type="number" value ="<?php echo 
    $stock;?>">
    <input type="hidden" name="product_id2" value="<?php echo 
    $product_id ?>"/>
    <button value="Submit" onclick="Submit" >Update Stock</button>
</form>

You never needed this file update-stock-success.php. Things in WP works little differently than Core_PHP.

发布评论

评论列表(0)

  1. 暂无评论