agraddy/wp-base-settings

允许轻松创建Wordpress设置页面。

0.1.11 2018-06-30 18:10 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:48:29 UTC


README

允许轻松创建Wordpress设置页面。

安装

composer require agraddy/wp-base-settings

原因

创建Wordpress设置页面需要大量工作。本库的目标是简化流程,让您可以快速输出带有自定义字段的自定义页面。

基本示例

您可以通过以下步骤创建一个非常简单的自定义插件设置页面

  • 在您的Wordpress /wp-content/plugins目录下创建一个名为custom-plugin的目录
  • 在/wp-content/plugins/custom-plugin目录中,使用composer安装agraddy/wp-base-settings(这将创建一个vendor目录)
  • 创建一个名为main.php的文件并添加以下代码。
  • 将短代码[cp_data_output]放在一个页面上以查看输出。
<?php
/**   
 * Plugin Name: Custom Plugin  
 * Description: A simple custom plugin to demonstrate creating a settings page.
 * Version: 1.0.0              
 */

require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');

class Custom_Plugin {
        public $key = 'cp';
        public $key_ = 'cp_';

        public $settings;
        public $page;

        public function __construct() {
                add_action('init', [$this, 'init']);

                // Create an instance of Settings.
                $this->settings = new \agraddy\base\Settings();

                // Set the key that needs to be used.
                $this->settings->config('key', $this->key);

                // Create the title for the page and the menu slug (used in the url)
                // In this example, it is: cp_settings
                $this->page = $this->settings->page('Custom Settings', $this->key_ . 'settings');

                // Create three different types of fields to save on the settings page.
                $this->page->add('text', 'First Example');
                $this->page->add('textarea', 'For Longer Text');
                $this->page->add('select_page', 'Pick A Page');
        }

        public function init() {
                // Add to the menu using typical Wordpress code (only allow admins to access):
                add_action('admin_menu', function() {
                        add_options_page('Custom Settings', 'Custom Settings', 'manage_options', $this->key_ . 'settings', [$this, 'customSettings']);
                });

                // Put the shortcode [cp_data_output] on a page to view the output.
                add_shortcode('cp_data_output', [$this, 'shortcodeDataOutput']);
        }

        public function customSettings() {
                // Output the html for the settings page.
                echo $this->page->html();
        }

        public function shortcodeDataOutput() {
                // The data is stored in Wordpress options.
                // It is prefixed by the key that was setup.
                // The name that was used for the field is converted to lowercase and spaces replace with underscores.
                $first_example = get_option('cp_first_example');
                $longer_text = get_option('cp_for_longer_text');
                $page_id = get_option('cp_pick_a_page');

                $output = '';
                $output .= 'First Example: ' . esc_html($first_example);
                $output .= '<br>';
                $output .= 'For Longer Text: ' . esc_html($longer_text);
                $output .= '<br>';
                $output .= 'Pick A Page: ' . esc_html($page_id);
                $output .= '<br>';

                return $output;
        }
}

new Custom_Plugin();

?>

注意

  • 主要设置代码位于__construct方法中(大部分其他代码是为了设置一个自定义插件以查看其工作)。
  • 这只是一个介绍页面,用于解释基础知识。如果您有任何问题,请提交一个issue。

许可证

MIT