Register metaboxes from plugin. WordPress example open

Register metaboxes from plugin. WordPress example

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

After creating your plugin, you can create new metaboxes immediately from the plugin

Add this code to the main plugin file pluginname.php or output to a separate file:

<?php
/*
Plugin Name: My Plugin
Plugin URI: https://mypluginsite.com/
Description: One of best plugins in the planet
Version: 1.0
Author: John
Author URI: https://facebook.com/john
Licence: GPLv2 or later
Text Domain: myplugin
Domain Path: /lang
 */
 
if (!defined('ABSPATH')) {
    die;
}
 

if (!class_exists('myPluginBoxes')) {

    class myPluginBoxes {

        public function register() {
            add_action('add_meta_boxes', [$this, 'add_meta_box_one']);
            add_action('save_post', [$this, 'save_matabox'], 10, 2);

        }

        public function add_meta_box_one() {
            add_meta_box(
                'one_settings', // id
                 'Property Settings', // section name
                [$this, 'metabox_one_html'], //output function
                 'posts', // posttype for our metaboxes
                 'normal', // priority
                 'default', // place to display our boxes
            );
        }

        public function save_matabox($post_id, $post) {
            //verify our nonce
            if (!isset($_POST['one_security']) || !wp_verify_nonce($_POST['one_security'], 'onefields')) {
                return;
            }

            //check autosave post
            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
                return $post_id;
            }

            //check post types
            if ($post->post_type != 'post') {
                return $post_id;
            }

            //check if the user can save posts
            $post_type = get_post_type_object($post->post_type);
            if (!current_user_can($post_type->cap->edit_post, $post_id)) {
                return $post_id;
            }

            // Check if there is data, if so, update the post if not delete them

            if (is_null($_POST['one_price'])) {
                delete_post_meta($post_id, 'one_price');
            } else {
                update_post_meta($post_id, 'one_price', sanitize_text_field(intval($_POST['one_price']))); //removes extra characters in addition to numbers
            }


            if (is_null($_POST['one_type'])) {
                delete_post_meta($post_id, 'one_type');
            } else {
                update_post_meta($post_id, 'one_type', sanitize_text_field($_POST['one_type']));
            }

            if (is_null($_POST['one_client'])) {
                delete_post_meta($post_id, 'one_client');
            } else {
                update_post_meta($post_id, 'one_client', sanitize_text_field($_POST['one_client']));
            }

            return $post_id;
        }

        public function metabox_one_html($post) {
            $price = get_post_meta($post->ID, 'one_price', true); //get post meta one_price from database
            $type = get_post_meta($post->ID, 'one_type', true); //get post meta one_type from database
            $client_meta = get_post_meta($post - ID, 'one_client', true);
            //security check
            wp_nonce_field('onefields', 'one_security'); 

            echo '
            <p>
                <label for="one_price">' . esc_html__('Price', 'myplugin') . '</label>
                <input type="number" id="one_price" name="one_price" value="' . esc_attr($price) . '">
            </p>


            <p>
                <label for="one_type">' . esc_html__('Type', 'myplugin') . '</label>
                <select id="one_type" name="one_type">
                    <option value="">Select</option>
                    <option value="sale" ' . selected('sale', $type, false) . '>' . esc_html__('For Sale', 'myplugin') . '</option>
                    <option value="rent" ' . selected('rent', $type, false) . '>' . esc_html__('For Rent', 'myplugin') . '</option>
                    <option value="sold" ' . selected('sold', $type, false) . '>' . esc_html__('Sold', 'mypligin') . '</option>
                </select>
            </p>
            ';

            $clients = get_posts(array('post_type' => 'clients', 'numberposts' => -1));

            if ($clients) {
                echo '<p>
                <label for="one_client">' . esc_html__('Client', 'myplugin') . '</label>
                <select id="one_client" name="one_client">
                    <option value="">' . esc_html__('Select Client', 'myplugin') . '</option>';

                foreach ($clients as $client) { ?>
                    <option value="<?php echo esc_html($client->ID); ?>" <?php if ($client->ID == $client_meta) {echo 'selected';} ?>><?php echo esc_html($agent->post_title); ?></option>
                <?php }

                echo '</select>
                </p>';
            }
        }

    }
}
if (class_exists('alePropertyCpt')) {
    $alePropertyCpt = new alePropertyCpt();
    $alePropertyCpt->register();
}
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation