wordpress 插件,wordpress 建站插件,wp 必备插件(图片说明)(如何创建WordPress插件设置页面?)
wordpress 插件,wordpress 建站插件,wp 必备插件(图片说明)
wordpress 插件,在WordPress中除了主题以外,最重要的就要属wordpress 插件了,在某些地方来说,甚至比主题更重要,wordpress 插件往往能够帮助网站实现一些特定的功能,让不懂代码的人也能为网站添加功能,从而避免花费时间和精力进行二次开发。wordpress 插件其中有些是对WordPress本身不足的修补,比如SEO优化插件、留言过滤插件、安全插件等;有些则是非常好用的增强插件,比如页面编辑器插件、幻灯片插件、弹窗插件等。
wordpress 插件修改网站页面是每个站长都会做的事情,比如新建一个定制的页面,添加一个图片、按钮之类的元素到网页上,但是用代码的话非常麻烦,所以wordpress 插件就出现了,所见即所得的使用方式让站长可以轻松的修改网页。wordpress 插件是目前最流行的WordPress可视化页面编辑插件,现代化的编辑方式和轻量的体验让wordpress 插件广受好评。
在新的版本中,wordpress 插件更是推出了主题编辑功能,wordpress 插件突破了之前用户不能改变主题的限制,让站长可以使用wordpress 插件设计自己的主题,可以让网站站长可视化设计页面,无需任何代码知识,使用wordpress 插件改造自己的主题,站长制作各种小工具,比如侧边栏小工具、区块等,替换单一功能的插件,比如幻灯片、表单、弹窗插件。
如果站长的网站想要获得更好的排名,那么SEO优化是必不可少的,虽然WordPress本身的SEO功能是非常不错的,但是比如标题、关键词都不能单独设置,需要使用wordpress 插件来强化,所以SEO插件通常都是必备的。
wordpress 插件是功能全面的WordPress SEO插件,它几乎提供了最全面的SEO功能,其中主要包括:网站主页TDK设置;详情页TDK设置;网站索引设置;站长工具;网站地图等。许多站长使用wordpress 插件设置网站的关键词和网站优化策略,能够帮助WordPress网站完善SEO功能,提高搜索引擎排名。
wordpress 插件最大的特点就是它会实时根据内容为你的文章提出SEO建议,比如文章的内容、关键词的设置、描述的设置,并且你可以设置每篇文章的社交分享信息,在优化方面也能够完成基本的功能,尤其对于个人或者企业站是必备插件。
wordpress 插件就是一款免费的Wordpress备份插件,它可以非常方便的备份WordPress站点,包括数据库、插件、主题、上传文件、其他文件等必要内容,点击按钮一键备份,站长可以将其备份下载到本地,也可以直接备份到FTP、Dropbox、OneDrive等云端存储,wordpress 插件还支持设置定时任务,自动备份你的站点,无需管理便能保证数据安全。
总之,wordpress 插件在保证网站功能的同时,避免安装无用的插件,保持网站的简洁、规整,提升性能,让网站使用起来更能流畅、体验更好,这样才能更加吸引搜索引擎的蜘蛛抓取网站和访问用户频繁的来到网站逗留。
如何创建WordPress插件设置页面?
很多开发者在创建新的WordPress插件的时候,需要在插件中创建一个设置页面,那么如何创建WordPress插件设置页面?接下来我们一起探讨一下。
WordPress设置API
WordPress 设置API是在WordPress 2.7引入的,允许开发人员在仪表板设置页面添加新的字段,或创建新的设置字段。WordPress会智能地显示并采取保存数据:
它使您可以定义设置页面,修改这些页面中的部分以及这些部分中的字段。
这是本文中讨论的方法中最手动的方法,但是值得了解它的工作方式,在某些情况下,这是一种可能非常简单的方法。
首先,我们需要注册一个新的菜单项和页面,其中将包含我们的设置表单。让我们在WordPress仪表板的“设置”顶级菜单项下添加页面:
1 2 3 4 5 6 |
<?php function dbi_add_settings_page() { add_options_page( \'Example plugin page\', \'Example Plugin Menu\', \'manage_options\', ‘dbi-example-plugin’, \'dbi_render_plugin_settings_page\' ); } add_action( \'admin_menu\', \'dbi_add_settings_page\' ); |
第五个参数add_options_page是用于显示页面内容的函数的名称,它将用于输出设置表单。该函数需要有一个表单元素和一些函数调用才能与设置API进行通信:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php function dbi_render_plugin_settings_page() { ?> <h3>Example Plugin Settings</h3> <form action=\"options.php\" method=\"post\"> <?php settings_fields( \'dbi_example_plugin_options\' ); do_settings_sections( \'dbi_example_plugin\' ); ?> <input name=\"submit\" class=\"button button-primary\" type=\"submit\" value=\"<?php esc_attr_e( \'Save\' ); ?>\" /> </form> <?php } |
函数settings_fields呈现代码以告知表单要执行的操作,以及隐藏输入以使用nonce使其安全。传递给函数的参数是设置组的名称,该名称将在以后注册。
函数do_settings_sections是表单的关键部分,在这里输出所有部分和字段(文本框、选择框、复选框等),以便用户可以输入数据。同样,该函数参数是任意的,但必须是唯一的。我们将在注册字段时使用它。
让我们这样做:
1 2 3 4 5 6 7 8 9 10 11 |
<?php function dbi_register_settings() { register_setting( \'dbi_example_plugin_options\', \'dbi_example_plugin_options\', \'dbi_example_plugin_options_validate\' ); add_settings_section( \'api_settings\', \'API Settings\', \'dbi_plugin_section_text\', \'dbi_example_plugin\' ); add_settings_field( \'dbi_plugin_setting_api_key\', \'API Key\', \'dbi_plugin_setting_api_key\', \'dbi_example_plugin\', \'api_settings\' ); add_settings_field( \'dbi_plugin_setting_results_limit\', \'Results Limit\', \'dbi_plugin_setting_results_limit\', \'dbi_example_plugin\', \'api_settings\' ); add_settings_field( \'dbi_plugin_setting_start_date\', \'Start Date\', \'dbi_plugin_setting_start_date\', \'dbi_example_plugin\', \'api_settings\' ); } add_action( \'admin_init\', \'dbi_register_settings\' ); |
这里发生了三件事。首先,我们使用register_setting为我们的设置在wp_options表中创建新记录,其中\'dbi_example_plugin_options\'作为option_name。
我们的个人设置将存储为序列化的数据数组。这非常适合将我们所有的插件设置集中在一个地方,但是,除非正确处理,否则数据序列化在迁移WordPress数据库时可能会导致问题。第三个参数是函数的名称,该函数处理在保存选项时验证输入的数据。
例如,确保API密钥是一个仅包含数字和字母的32字符串:
1 2 3 4 5 6 7 8 9 10 |
<?php function dbi_example_plugin_options_validate( $input ) { $newinput[\'api_key\'] = trim( $input[\'api_key\'] ); if ( ! preg_match( \'/^[a-z0-9]{32}$/i\', $newinput[\'api_key\'] ) ) { $newinput[\'api_key\'] = \'\'; } return $newinput; } |
其次,我们正在注册一个与选项绑定的组部分。最后,我们使用add_settings_field来为该组注册三个字段。所有这些都是用于保存插件设置的文本字段。
接下来,我们需要创建函数以显示该部分的有用文本并在表单上呈现字段:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php function dbi_plugin_section_text() { echo \'<p>Here you can set all the options for using the API</p>\'; } function dbi_plugin_setting_api_key() { $options = get_option( \'dbi_example_plugin_options\' ); echo \"<input id=\'dbi_plugin_setting_api_key\' name=\'dbi_example_plugin_options[api_key]\' type=\'text\' value=\'{esc_attr( $options[\'api_key\'] )}\' />\"; } function dbi_plugin_setting_results_limit() { $options = get_option( \'dbi_example_plugin_options\' ); echo \"<input id=\'dbi_plugin_setting_results_limit\' name=\'dbi_example_plugin_options[results_limit]\' type=\'text\' value=\'{esc_attr( $options[\'results_limit\'] )}\' />\"; } function dbi_plugin_setting_start_date() { $options = get_option( \'dbi_example_plugin_options\' ); echo \"<input id=\'dbi_plugin_setting_start_date\' name=\'dbi_example_plugin_options[start_date]\' type=\'text\' value=\'{esc_attr( $options[\'start_date\'] )}\' />\"; } |
就是这样,然后我们就有了一个设置页面:
我们要在代码中使用这里设置值的时候,可以通过使用get_option(\'dbi_example_plugin_options\')来获取这些设置的数组数据。
自定义字段框架
谈到PHP框架,我想展示一种无需使用WordPress设置API即可创建设置页面的替代方法。在为客户端构建站点时,如果需要设置页面,则默认使用“Advanced Custom Fields”插件(简称ACF)创建选项页面,并使用它来定义字段。
这是我喜欢使用ACF的众多原因之一:它使复杂的WordPress变得简单。(您是否看过ACF Blocks,它是一个PHP包装器,可在不接触React的情况下为古腾堡编辑器注册块?)
但是,在构建用于分发的插件或主题时,您不能依赖安装了ACF的用户。在这里我推荐“Carbon Fields”框架。与ACF不同,Carbon Fields不是插件,通常作为库安装在插件内部,推荐的方法是使用Composer。
提示:如果确实使用Composer在插件中安装Carbon Fields,请记住通过为Composer软件包的名称空间加上前缀来避免WordPress依赖关系管理。
安装完成后,首先确保引入 Composer的autoload.php文件,然后使用下面的函数启动Carbon Fields库:
1 2 3 4 5 6 |
<?php function dbi_load_carbon_fields() { \\Carbon_Fields\\Carbon_Fields::boot(); } add_action( \'after_setup_theme\', \'dbi_load_carbon_fields\' ); |
要复制我们使用WordPress设置API所做的工作,只需要使用下面简单的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php use Carbon_Fields\\Container; use Carbon_Fields\\Field; function dbi_add_plugin_settings_page() { Container::make( \'theme_options\', __( \'Example Plugin Page\' ) ) ->set_page_parent( \'options-general.php\' ) ->add_fields( array( Field::make( \'text\', \'dbi_api_key\', \'API Key\' ) ->set_attribute( maxLength, 32 ), Field::make( \'text\', \'dbi_results_limit\', \'Results Limit\' ) ->set_attribute( \'min\', 1 ) ->set_attribute( \'max\', 100 ) ->set_default_value( 10 ), Field::make( \'date\', \'dbi_start_date\', \'Start Date\' ), ) ); } add_action( \'carbon_fields_register_fields\', \'dbi_add_plugin_settings_page\' ); |
如您所见,该框架使将字段变得更加用容易和友好,例如将默认值、最小值和最大值添加到“结果限制”字段中,以及将“开始日期”字段转换为日期选择器–使用设置API需要一些编码才能实现。
要使用该设置保存的数据,您需要使用以下carbon_get_theme_option()函数:
1 2 3 |
<?php $api_key = carbon_get_theme_option( \'dbi_api_key\' ); |
框架可以使用更少的代码来实现功能,让您更专注于编写插件代码,而不是重头开始编写设置部分的代码。添加页面
使用代码生成器
另一种比WordPress设置API、以及框架更简便的方法是使用以下WordPress生成器之一。
让我们使用WordPress选项页面生成器重新创建设置页面。它仅支持少数字段类型。文字、文本域、选择、单选、复选框,没有日期选择器。
以下是通过生成器生成的代码,可以直接在插件中使用,并以与WordPress设置API相同的方式使用数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
<?php /** * Generated by the WordPress Option Page generator * at http://jeremyhixon.com/wp-tools/option-page/ */ class MyExamplePlugin { private $my_example_plugin_options; public function __construct() { add_action( \'admin_menu\', array( $this, \'my_example_plugin_add_plugin_page\' ) ); add_action( \'admin_init\', array( $this, \'my_example_plugin_page_init\' ) ); } public function my_example_plugin_add_plugin_page() { add_options_page( \'My Example Plugin\', // page_title \'My Example Plugin\', // menu_title \'manage_options\', // capability \'my-example-plugin\', // menu_slug array( $this, \'my_example_plugin_create_admin_page\' ) // function ); } public function my_example_plugin_create_admin_page() { $this->my_example_plugin_options = get_option( \'my_example_plugin_option_name\' ); ?> <div class=\"wrap\"> <h3>My Example Plugin</h3> <p></p> <?php settings_errors(); ?> <form method=\"post\" action=\"options.php\"> <?php settings_fields( \'my_example_plugin_option_group\' ); do_settings_sections( \'my-example-plugin-admin\' ); submit_button(); ?> </form> </div> <?php } public function my_example_plugin_page_init() { register_setting( \'my_example_plugin_option_group\', // option_group \'my_example_plugin_option_name\', // option_name array( $this, \'my_example_plugin_sanitize\' ) // sanitize_callback ); add_settings_section( \'my_example_plugin_setting_section\', // id \'Settings\', // title array( $this, \'my_example_plugin_section_info\' ), // callback \'my-example-plugin-admin\' // page ); add_settings_field( \'api_key_0\', // id \'API Key\', // title array( $this, \'api_key_0_callback\' ), // callback \'my-example-plugin-admin\', // page \'my_example_plugin_setting_section\' // section ); add_settings_field( \'results_limit_1\', // id \'Results Limit\', // title array( $this, \'results_limit_1_callback\' ), // callback \'my-example-plugin-admin\', // page \'my_example_plugin_setting_section\' // section ); add_settings_field( \'start_date_2\', // id \'Start Date\', // title array( $this, \'start_date_2_callback\' ), // callback \'my-example-plugin-admin\', // page \'my_example_plugin_setting_section\' // section ); } public function my_example_plugin_sanitize($input) { $sanitary_values = array(); if ( isset( $input[\'api_key_0\'] ) ) { $sanitary_values[\'api_key_0\'] = sanitize_text_field( $input[\'api_key_0\'] ); } if ( isset( $input[\'results_limit_1\'] ) ) { $sanitary_values[\'results_limit_1\'] = sanitize_text_field( $input[\'results_limit_1\'] ); } if ( isset( $input[\'start_date_2\'] ) ) { $sanitary_values[\'start_date_2\'] = sanitize_text_field( $input[\'start_date_2\'] ); } return $sanitary_values; } public function my_example_plugin_section_info() { } public function api_key_0_callback() { printf( \'<input class=\"regular-text\" type=\"text\" name=\"my_example_plugin_option_name[api_key_0]\" id=\"api_key_0\" value=\"%s\">\', isset( $this->my_example_plugin_options[\'api_key_0\'] ) ? esc_attr( $this->my_example_plugin_options[\'api_key_0\']) : \'\' ); } public function results_limit_1_callback() { printf( \'<input class=\"regular-text\" type=\"text\" name=\"my_example_plugin_option_name[results_limit_1]\" id=\"results_limit_1\" value=\"%s\">\', isset( $this->my_example_plugin_options[\'results_limit_1\'] ) ? esc_attr( $this->my_example_plugin_options[\'results_limit_1\']) : \'\' ); } public function start_date_2_callback() { printf( \'<input class=\"regular-text\" type=\"text\" name=\"my_example_plugin_option_name[start_date_2]\" id=\"start_date_2\" value=\"%s\">\', isset( $this->my_example_plugin_options[\'start_date_2\'] ) ? esc_attr( $this->my_example_plugin_options[\'start_date_2\']) : \'\' ); } } if ( is_admin() ) $my_example_plugin = new MyExamplePlugin(); /* * Retrieve this value with: * $my_example_plugin_options = get_option( \'my_example_plugin_option_name\' ); // Array of All Options * $api_key_0 = $my_example_plugin_options[\'api_key_0\']; // API Key * $results_limit_1 = $my_example_plugin_options[\'results_limit_1\']; // Results Limit * $start_date_2 = $my_example_plugin_options[\'start_date_2\']; // Start Date */ |
生成器是一个非常有用的工具,可通过一个现成的设置页面启动您的插件,无需进行大量编码即可对其进行调整。它们还可以帮助您学习代码的工作原理,分离现有代码并查看它们如何组合在一起。但是,从长远来看,对于需要对设置代码进行大量更改的插件来说,它们可能不是最佳选择。
使用REST API
WordPress REST API的许多重要用途之一就是改善您的插件或主题设置屏幕。
REST API可以提供优于传统admin-ajax.php请求的性能优势,再加上单击“保存”无需刷新页面即可在后台保存表单设置,这种方法比WordPress设置API更友好。
Josh指出,随着添加设置和表单变得越来越复杂,“使用jQuery将会做得越来越多,这很难管理而且会更简单,并且如果您使用VueJS或React可以提供更好的用户体验” ,这使我很好地了解了下一个方法,即VueJS支持的设置页面。
使用VueJS
在去年收购WP User Manager插件的过程中,我调查了代码库以了解其编写方式,并发现设置页面由使用REST API和VueJS的软件包 –wp-optionskit提供支持 :
该软件包可随Composer一起安装,并使用此处的代码实例化和配置设置页面。这是我们的示例设置页面的外观:
我喜欢这种方法的地方在于,它允许您仅通过在PHP中配置数据数组来生成具有多个选项卡和小节的复杂设置页面。与其他方法一样,设置数据存储在选项表的一条记录中,因此这是一种检索和使用保存的数据的熟悉方法。
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » wordpress 插件,wordpress 建站插件,wp 必备插件(图片说明)(如何创建WordPress插件设置页面?)