Daansk44
Daansk44

Reputation: 497

custom fields (in a custom page) are not saved (wordpress) (complete code on bottom)

I'm writing a plugin to create custom slugs, boxes and text blocks that allow you to save the data in the Wordpress database. For example, a products custom pages, which you can give values.

I've made three files.

On this moment I am hanging on the mediabox.php code.

It does not want to save the values of the boxes and input fields into the database.

I have documented everything in the PHP document so that you might be able to help me a little better :) (Everyone is free to provide feedback :)).

I start my class with a namespace with the class media box. That's included $name (name of the box), $post (to get the postID) and the $output (generates the html)

In the constructor, I give add the $post so it can call itself in a function.

 namespace App\controller\build;

class mediabox
{

    protected $name;
    protected $post;
    protected $output = "";

    /**
     * mediabox constructor.
     * @param $name
     */
    public function __construct($post, $name)
    {
        // In de __construct roepen wij de add_meta_boxes aan, die de boxen dienen aan te maken
        $this->name = $name;
        $this->post = $post;
        add_action('add_meta_boxes', [$this, 'api_add_meta_boxes']);
//Save function
        if (!isset($_POST['api_meta_box_nonce']) || !wp_verify_nonce($_POST['api_meta_box_nonce'], basename(__FILE__))) {
            return;
        }
        // return if autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }
        // Check the user's permissions.
        if (!current_user_can('edit_post', $post)) {


             return;
            }
  update_post_meta($this->post, '_api_UserEnabled', 'aaaaa');
    }

In the mini code above I call an update function that does not work properly.

/**
     * @param $name
     * @return mediabox
     * Hier wordt de factorie functie aangeroepen
     */
    public static function factory($post, $name)
    {
        return new self($post, $name);
    }

In addition, I have created a factory that calls itself.

After this I call the add_meta_boxes

/**
 * @param $name
 */
public function api_add_meta_boxes($name)
{
    // Roept api_meta_box aan
    add_meta_box('api_meta_box', __($name, 'api_example_plugin'), [$this, 'api_build_meta_box'], $this->name, 'normal', 'high');
    wp_nonce_field(basename(__FILE__), 'api_meta_box_nonce');
}

And finally, I make the function to generate the boxes and text fields.

/**
     * @param $post
     * @param $build_name
     * @return $this
     * In deze functie kan een radiobutton worden aangemaakt, hier dient een array aan toegevoegd te worden
     */
    public function build_radio($build_name, $array)
    {
        $current_UserEnabled = get_post_meta($this->post->ID, '_api_UserEnabled', true);
        $this->output .= " 
        <h3>{$build_name} </h3>";
        foreach ($array as $key) {
            $this->output .= " 
            <input type='radio' name='{$build_name}' value = '{$key}'" . checked($current_UserEnabled, $key) . ">{$key}<br>";
            ?>

            </div>
            <?php
            # code...
        }
        return $this;
    }

    /**
     * @param $post
     * @param $build_name
     * @return $this
     */
    public function build_textbox($build_name)
    {
        $current_Username = get_post_meta($this->post->ID, $build_name, true);
        $this->output .= "<h3>{$build_name}</h3><p><input type='text' name='{$build_name}' value='{$current_Username}'/></p>";


        return $this;
    }

    /**
     * @param $post
     * @param $build_name
     * @return $this
     */
    public function build_selectbox($build_name, $userinfo)
    {
        $this->output .= " 
        <h3>{$build_name} </h3>";
        foreach ($userinfo as $myuserinfo) {
            $this->output .= "<p>{$myuserinfo}</p>";
            $this->output .= "<input type=checkbox name='{$build_name}' value='{$myuserinfo}' " . checked((in_array($build_name, $myuserinfo)) ? $myuserinfo : '', $myuserinfo) . ">";
        }
        ?>
        </p>
        <?php
        return $this;
    }

    /**
     * @param $post
     * @return $this
     */
    public function api_build_meta_box($post)
    {

        echo $this->output;
        return $this;
        var_dump($_post);


    }

}

When I call the update_post_meta with a costum value ( update_post_meta($this->post, '_api_UserEnabled', 'aaaaa');) it does not work :/.

enter image description here

Complete code

structure

enter image description here

WPAUTH2.php (main plug-in file)

/*
Plugin Name: WPAUTH2
Plugin URI: https://xxx
Description: AUTH SERVER
Version: 1.2
Author: Daan Seegers
Author URI: https://xxx
License: GPLv2 or later
 */
require_once 'vendor/autoload.php';
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);




new \App\controller\register\new_register_post_type('API', 'oauth-api');

add_action('init', function () {
    if (!isset($_GET['post']) || !is_admin()) {
        return;
    }
    $post = get_post(intval($_GET['post']));
    \App\controller\build\mediabox::factory($post, 'oauth-api')
        ->build_textbox('Tralalala')->build_selectbox('brah' , array("gmx", "sss", "ssaa"))->build_radio("hello", array("Volvo", "BMW", "Toyota"));

});

new_register_post_type.php

<?php
/**
 * Register taxonomies
 */

/**
 * Add meta box
 *
 * @param post $post The post object
 * @link https://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes
 */

namespace App\controller\register;

class new_register_post_type
{
    private $name;
    private $slug;

    public function __construct($name, $slug)
    {
        $this->name = $name;
        $this->slug = $slug;

        add_action('init', [$this, 'API_setup']);

    }

    public function API_setup($name)
    {
        $labels = array(
            'name' => __($this->name, $this->name . '_example_plugin'),
            'singular_name' => __($this->name, $this->name . '_example_plugin'),
            'add_new_item' => __('Add New ' . $this->name, $this->name . '_example_plugin'),
            'edit_item' => __('Edit ' . $this->name, $this->name . '_example_plugin'),
            'new_item' => __('New ' . $this->name, $this->name . '_example_plugin'),
            'not_found' => __('No ' . $this->name . ' found', $this->name . '_example_plugin'),
            'all_items' => __('All ' . $this->name, $this->name . '_example_plugin'),
        );
        $args = array(
            'labels' => $labels,
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'has_archive' => true,
            'map_meta_cap' => true,
            'menu_icon' => 'dashicons-performance',
            'supports' => array('title'),

        );
        register_post_type($this->slug, $args);
    }

}

Mediabox

<?php

namespace App\controller\build;

class mediabox
{

    protected $name;
    protected $post;
    protected $output = "";

    /**
     * mediabox constructor.
     * @param $name
     */
    public function __construct($post, $name)
    {
        // In de __construct roepen wij de add_meta_boxes aan, die de boxen dienen aan te maken
        $this->name = $name;
        $this->post = $post;
        add_action('add_meta_boxes', [$this, 'api_add_meta_boxes']);
//Save function
        if (!isset($_POST['api_meta_box_nonce']) || !wp_verify_nonce($_POST['api_meta_box_nonce'], basename(__FILE__))) {
            return;
        }
        // return if autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return;
        }
        // Check the user's permissions.
        if (!current_user_can('edit_post', $post)) {
            return;
        }


    update_post_meta($this->post->ID, '_api_UserEnabled', 'aaaaa');
}

    /**
     * @param $name
     * @return mediabox
     * Hier wordt de factorie functie aangeroepen
     */
    public static function factory($post, $name)
    {
        return new self($post, $name);
    }

    /**
     * @param $name
     */
    public function api_add_meta_boxes($name)
    {
        // Roept api_meta_box aan
        add_meta_box('api_meta_box', __($name, 'api_example_plugin'), [$this, 'api_build_meta_box'], $this->name, 'normal', 'high');
        wp_nonce_field(basename(__FILE__), 'api_meta_box_nonce');
    }

    /**
     * @param $post
     * @param $build_name
     * @return $this
     * In deze functie kan een radiobutton worden aangemaakt, hier dient een array aan toegevoegd te worden
     */
    public function build_radio($build_name, $array)
    {
        $current_UserEnabled = get_post_meta($this->post->ID, '_api_UserEnabled', true);
        $this->output .= " 
        <h3>{$build_name} </h3>";
        foreach ($array as $key) {
            $this->output .= " 
            <input type='radio' name='{$build_name}' value = '{$key}'" . checked($current_UserEnabled, $key) . ">{$key}<br>";
            ?>

            </div>
            <?php
            # code...
        }
        return $this;
    }

    /**
     * @param $post
     * @param $build_name
     * @return $this
     */
    public function build_textbox($build_name)
    {
        $current_Username = get_post_meta($this->post->ID, $build_name, true);
        $this->output .= "<h3>{$build_name}</h3><p><input type='text' name='{$build_name}' value='{$current_Username}'/></p>";


        return $this;
    }

    /**
     * @param $post
     * @param $build_name
     * @return $this
     */
    public function build_selectbox($build_name, $userinfo)
    {
        $this->output .= " 
        <h3>{$build_name} </h3>";
        foreach ($userinfo as $myuserinfo) {
            $this->output .= "<p>{$myuserinfo}</p>";
            $this->output .= "<input type=checkbox name='{$build_name}' value='{$myuserinfo}' " . checked((in_array($build_name, $myuserinfo)) ? $myuserinfo : '', $myuserinfo) . ">";
        }
        ?>
        </p>
        <?php
        return $this;
    }

    /**
     * @param $post
     * @return $this
     */
    public function api_build_meta_box($post)
    {

        echo $this->output;
        return $this;
        var_dump($_post);


    }

}

Upvotes: 0

Views: 58

Answers (1)

Vel
Vel

Reputation: 9317

you need to use

update_post_meta($this->post->ID, '_api_UserEnabled', 'aaaaa'); 

instead of

update_post_meta($this->post, '_api_UserEnabled', 'aaaaa'); 

Upvotes: 0

Related Questions