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: update-stock-success.php is taken to the wordpress frontend

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!

| improve this answer | |
  • I tried adding it using add_submenu_page but with no luck i get directed to the frontend i.e.(from domain.com/wp-admin/admin.php?page=update-stock to domain.com/wp-content/plugins/plugin-name/inc/views/update-stock-success.php). I want to stay on admin backend only. – Savio menezes Jan 13 at 11:25
  • Can you please share your code or if it is on git please share the git url? – BlueSuiter Jan 14 at 8:35
  • I have updated question by adding actual code and the domain has been blurred for safety reasons.Also plugin folder is blurred and hence should be referred as plugin-name. – Savio menezes Jan 15 at 4:45
0

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.

| improve this answer | |
  • Thanks a lot for taking time on this. Sorry, It was a typo. It is update-stock as slug and update-stockfunc in submenu. I wanted to reduce the update-stock.php so that i want to go to different page on success and fail resp. Please help, problem still persists. – Savio menezes Jan 15 at 10:03
  • 1) You can keep update-stock.php for showing forms and can use main file for defining methods. 2) Also, I forgot to mention in answer that you just had to update the second form on update-stock.php can keep the first one as it is. 3) You don't needed update-stock-success.php because the functionality performed in this file should be handled in a method not as you did. – BlueSuiter Jan 15 at 12:07
  • Thanks again. Could please guide me on how to use functions for wordpress plugin backend using functions with an example. – Savio menezes Jan 17 at 10:30

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.