php这个wordpress插件演示了如何使用wordpress提供的可拖动元文件构建自己的插件页面,需要wordpr(代码片段)

author author     2022-12-12     116

关键词:

<?php
/*
Plugin Name: HowTo Plugin
Plugin URI: 
Description: This Plugin demonstrates how you can build your own plugin pages using the WordPress provided draggable metaboxes, requires WordPress 2.7 version, supports WordPress 2.8 changed boxing layout engine
Author: Heiko, Frank
Author URI: http://bueltge.de
Version: 0.1

License:
 ==============================================================================
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

//avoid direct calls to this file where wp core files not present
if (!function_exists ('add_action')) 
		header('Status: 403 Forbidden');
		header('HTTP/1.1 403 Forbidden');
		exit();


define('HOWTO_METABOX_ADMIN_PAGE_NAME', 'howto_metaboxes');

//class that reperesent the complete plugin
class howto_metabox_plugin 

	//constructor of class, PHP4 compatible construction for backward compatibility
	function howto_metabox_plugin() 
		//add filter for WordPress 2.8 changed backend box system !
		add_filter('screen_layout_columns', array(&$this, 'on_screen_layout_columns'), 10, 2);
		//register callback for admin menu  setup
		add_action('admin_menu', array(&$this, 'on_admin_menu')); 
		//register the callback been used if options of page been submitted and needs to be processed
		add_action('admin_post_save_howto_metaboxes_general', array(&$this, 'on_save_changes'));
	
	
	//for WordPress 2.8 we have to tell, that we support 2 columns !
	function on_screen_layout_columns($columns, $screen) 
		if ($screen == $this->pagehook) 
			$columns[$this->pagehook] = 2;
		
		return $columns;
	
	
	//extend the admin menu
	function on_admin_menu() 
		//add our own option page, you can also add it to different sections or use your own one
		$this->pagehook = add_options_page('Howto Metabox Page Title', "HowTo Metaboxes", 'manage_options', HOWTO_METABOX_ADMIN_PAGE_NAME, array(&$this, 'on_show_page'));
		//register  callback gets call prior your own page gets rendered
		add_action('load-'.$this->pagehook, array(&$this, 'on_load_page'));
	
	
	//will be executed if wordpress core detects this page has to be rendered
	function on_load_page() 
		//ensure, that the needed javascripts been loaded to allow drag/drop, expand/collapse and hide/show of boxes
		wp_enqueue_script('common');
		wp_enqueue_script('wp-lists');
		wp_enqueue_script('postbox');

		//add several metaboxes now, all metaboxes registered during load page can be switched off/on at "Screen Options" automatically, nothing special to do therefore
		add_meta_box('howto-metaboxes-sidebox-1', 'Sidebox 1 Title', array(&$this, 'on_sidebox_1_content'), $this->pagehook, 'side', 'core');
		add_meta_box('howto-metaboxes-sidebox-2', 'Sidebox 2 Title', array(&$this, 'on_sidebox_2_content'), $this->pagehook, 'side', 'core');
		add_meta_box('howto-metaboxes-contentbox-1', 'Contentbox 1 Title', array(&$this, 'on_contentbox_1_content'), $this->pagehook, 'normal', 'core');
		add_meta_box('howto-metaboxes-contentbox-2', 'Contentbox 2 Title', array(&$this, 'on_contentbox_2_content'), $this->pagehook, 'normal', 'core');
		add_meta_box('howto-metaboxes-contentbox-additional-1', 'Contentbox Additional 1 Title', array(&$this, 'on_contentbox_additional_1_content'), $this->pagehook, 'additional', 'core');
		add_meta_box('howto-metaboxes-contentbox-additional-2', 'Contentbox Additional 2 Title', array(&$this, 'on_contentbox_additional_2_content'), $this->pagehook, 'additional', 'core');
	
	
	//executed to show the plugins complete admin page
	function on_show_page() 
		//we need the global screen column value to beable to have a sidebar in WordPress 2.8
		global $screen_layout_columns;
		//add a 3rd content box now for demonstration purpose, boxes added at start of page rendering can't be switched on/off, 
		//may be needed to ensure that a special box is always available
		add_meta_box('howto-metaboxes-contentbox-3', 'Contentbox 3 Title (impossible to hide)', array(&$this, 'on_contentbox_3_content'), $this->pagehook, 'normal', 'core');
		//define some data can be given to each metabox during rendering
		$data = array('My Data 1', 'My Data 2', 'Available Data 1');
		?>
		<div id="howto-metaboxes-general" class="wrap">
		<?php screen_icon('options-general'); ?>
		<h2>Metabox Showcase Plugin Page</h2>
		<form action="admin-post.php" method="post">
			<?php wp_nonce_field('howto-metaboxes-general'); ?>
			<?php wp_nonce_field('closedpostboxes', 'closedpostboxesnonce', false ); ?>
			<?php wp_nonce_field('meta-box-order', 'meta-box-order-nonce', false ); ?>
			<input type="hidden" name="action" value="save_howto_metaboxes_general" />
		
			<div id="poststuff" class="metabox-holder<?php echo 2 == $screen_layout_columns ? ' has-right-sidebar' : ''; ?>">
				<div id="side-info-column" class="inner-sidebar">
					<?php do_meta_boxes($this->pagehook, 'side', $data); ?>
				</div>
				<div id="post-body" class="has-sidebar">
					<div id="post-body-content" class="has-sidebar-content">
						<?php do_meta_boxes($this->pagehook, 'normal', $data); ?>
						<h4>Static text and input section</h4>
						<p>Here is some static paragraph or your own static content. Can be placed where ever you want.</p>
						<textarea name="static-textarea" style="width:100%;">Change this text ....</textarea>
						<br/>
						<?php do_meta_boxes($this->pagehook, 'additional', $data); ?>
						<p>
							<input type="submit" value="Save Changes" class="button-primary" name="Submit"/>	
						</p>
					</div>
				</div>
				<br class="clear"/>
								
			</div>	
		</form>
		</div>
	<script type="text/javascript">
		//<![CDATA[
		jQuery(document).ready( function($) 
			// close postboxes that should be closed
			$('.if-js-closed').removeClass('if-js-closed').addClass('closed');
			// postboxes setup
			postboxes.add_postbox_toggles('<?php echo $this->pagehook; ?>');
		);
		//]]>
	</script>
		
		<?php
	

	//executed if the post arrives initiated by pressing the submit button of form
	function on_save_changes() 
		//user permission check
		if ( !current_user_can('manage_options') )
			wp_die( __('Cheatin&#8217; uh?') );			
		//cross check the given referer
		check_admin_referer('howto-metaboxes-general');
		
		//process here your on $_POST validation and / or option saving
		
		//lets redirect the post request into get request (you may add additional params at the url, if you need to show save results
		wp_redirect($_POST['_wp_http_referer']);		
	

	//below you will find for each registered metabox the callback method, that produces the content inside the boxes
	//i did not describe each callback dedicated, what they do can be easily inspected and compare with the admin page displayed
	
	function on_sidebox_1_content($data) 
		?>
		<ul style="list-style-type:disc;margin-left:20px;">
			<?php foreach($data as $item)  echo "<li>$item</li>";  ?>
		</ul>
		<?php
	
	function on_sidebox_2_content($data) 
		?>
		<p>You can also use static text or any markup to be shown inside the boxes.</p>
		<?php
	
function on_contentbox_1_content($data) 
	sort($data);
	?>
		<p>The given parameter at <b>sorted</b> order are: <em><?php echo implode(' | ', $data); ?></em></p>
	<?php

	function on_contentbox_2_content($data) 
		sort($data);
		?>
		<p>The given parameter at <b>reverse sorted</b> order are: <em><?php echo implode(' | ', array_reverse($data)); ?></em></p>
		<?php
	
	function on_contentbox_3_content($data) 
		?>
		<p>This metabox can be dragged an placed where ever you want but <b>can't be hidden</b> using the <em>Screen Options</em> tab slider!</p>
		<?php
	
	function on_contentbox_additional_1_content($data) 
		?>
		<p>This and the 2nd <em>additional</em> box will be addressed by an other group identifier to render it by calling with this dedicated name.</p>
		<p>You can have as much as needed box groups.</p>
		<?php
	
	function on_contentbox_additional_2_content($data) 
		?>
			<p>metabox showcase - copyright &copy; 2009 Heiko Rabe (<a target="_blank" href="http://www.code-styling.de">www.code-styling.de</a>)</p>
			<p>requires at least WordPress 2.7 version, supports new box management of WordPress 2.8</p>
		<?php
	
	


$my_howto_metabox_plugin = new howto_metabox_plugin();

?>

如何在 Wordpress 插件中正确重定向?

】如何在Wordpress插件中正确重定向?【英文标题】:HowtoproprelyredirectinWordpressplugin?【发布时间】:2021-08-1014:03:13【问题描述】:我在Wordpress中为我的网站创建了一个新插件(PalaisBDD),但遇到了问题:首先,我有2个文件:PalaisBDD.ph... 查看详情

如何开发一个wordpress插件

参考技术A1首先安装wordpress环境,安装成功后访问如下2在wp-content\plugins目录下新建自己要创建的插件名称,这个名称需要是唯一的,3编辑mytest_helloword.php文件如下,可以看出来我们的插件的作用是修改文章中的Welcome为haha:4... 查看详情

如何开发一个wordpress插件

工具:文本编辑器、wordpress源码、浏览器、apachemysql环境步骤:1.首先安装wordpress环境,安装成功后访问如下。2.在wp-content\\plugins目录下新建自己要创建的插件名称,这个名称需要是唯一的,如下图:3.编辑mytest_helloword.php文件如下... 查看详情

如何在 WordPress 中使用 jQuery Cycle 插件?

】如何在WordPress中使用jQueryCycle插件?【英文标题】:HowtousethejQueryCyclePluginwithWordPress?【发布时间】:2010-10-2921:15:31【问题描述】:我已经尝试了我能想到的一切。不应该这么难。谁能给我解释一下使用jQuery和WordPress(特别是jQue... 查看详情

如何使用 php 在 WordPress 插件中回显 json 数据 [重复]

】如何使用php在WordPress插件中回显json数据[重复]【英文标题】:HowtoechojsondatainWordPresspluginusingphp[duplicate]【发布时间】:2020-09-0307:31:48【问题描述】:我正在创建一个Courier跟踪插件并使用他们的API获取数据。它们返回的输出是JSO... 查看详情

如何在 Wordpress 插件中使用 ReactJS

】如何在Wordpress插件中使用ReactJS【英文标题】:HowToUseReactJSinWordpressPlugin【发布时间】:2016-02-1610:08:44【问题描述】:我在Wordpress插件中使用ReactJs时遇到问题。我正在使用ReactJs开发一个Wordpress插件。谷歌搜索仅在Wordpress主题中... 查看详情

我在wordpress表单插件contactform7的使用上遇到问题,启用了这个插件,就是不显示。

我在wordpress表单插件ContactForm7的使用上遇到问题,启用了这个插件,就是不显示。怎么设置在首页弹出之前就让这个表单,先弹出来让用户留下联系方法呢?你回答的视频地址。我不知为什么没法看了。能帮帮我吗?哈哈,就像... 查看详情

如何使用带有 php 的cropit jquery 插件裁剪和上传照片

...时间】:2015-03-2101:43:53【问题描述】:所以我目前发现了这个名为cropit的照片裁剪插件。演示是here。所以我想要做的是抓取裁剪的照片并将照片的名称上传到mysql数据库并使用php将其保存到目录中。到目前为止,我有这个:HTM 查看详情

使用 PHP 从 WordPress 插件写入文本文件

】使用PHP从WordPress插件写入文本文件【英文标题】:WritefromWordPressplugintotextfilewithPHP【发布时间】:2014-10-2223:19:24【问题描述】:我正在尝试从WordPress插件将日期写入文本文件。虽然这适用于单个PHP文件,但当我将代码添加到插... 查看详情

WordPress 插件开发 - 如何使用 JQuery / JavaScript?

】WordPress插件开发-如何使用JQuery/JavaScript?【英文标题】:WordPressPluginDevelopment-HowtouseJQuery/JavaScript?【发布时间】:2013-05-2506:59:31【问题描述】:刚开始为WordPress开发插件,想在插件管理界面中使用一些JQuery。如何正确包含和调... 查看详情

wordpress如何设置多种支付方式

参考技术A关于wp的国内支付,一直都是很让人头疼的问题;因为是外国人开发的,所以对国内支付的插件,网上查询的结果是需要手动写代码实现但是,对于我这样不了解PHP的人,这个就比较难。经过多次实验,我曾经试过无数... 查看详情

将 jQuery 插件安装到 wordpress 中

】将jQuery插件安装到wordpress中【英文标题】:InstallingjQuerypluginintowordpress【发布时间】:2013-05-1109:10:22【问题描述】:我有一个用户将根据体能测试结果填写的表单,即每分钟俯卧撑、每分钟仰卧起坐、2英里跑步时间等。此表单... 查看详情

Wordpress 自定义表格插件

】Wordpress自定义表格插件【英文标题】:WordpressCustomtablesplugin【发布时间】:2012-10-2703:07:28【问题描述】:当我为Wordpress使用“customtables”插件时,如何解决这个警告?谁能帮帮我警告:在第378行的..\\wp-content\\plugins\\custom-tables\... 查看详情

如何防止 wordpress 插件出现在某些页面上

】如何防止wordpress插件出现在某些页面上【英文标题】:Howtopreventwordpresspluginsfromappearingoncertainpages【发布时间】:2015-02-0211:17:44【问题描述】:我注意到几乎每个插件在安装后都会在每个页面中显示/实现其代码(javascripts/css)。... 查看详情

使用 count() 修复 WordPress 插件文件中的 php sizeof 警告/错误

】使用count()修复WordPress插件文件中的phpsizeof警告/错误【英文标题】:Fixingphpsizeofwarning/errorinWordPresspluginfilewithcount()【发布时间】:2021-12-1101:40:21【问题描述】:我在一个网站上使用HierarchicalPages插件,我注意到如果我切换到较... 查看详情

Wordpress 插件 HTML Bootstrap + PHP

】Wordpress插件HTMLBootstrap+PHP【英文标题】:WordpressPluginHTMLBootstrap+PHP【发布时间】:2020-10-0621:44:07【问题描述】:我正在创建我的第一个wordpress插件来在wp-admin-page上显示一些仪表板数据,但是当我编辑我的插件时只有我的php代码... 查看详情

如何在 Wordpress 插件中加载 Javascript

】如何在Wordpress插件中加载Javascript【英文标题】:howtoloadJavascriptinWordpressPlugin【发布时间】:2012-02-2620:27:01【问题描述】:谁能告诉我如何将此javascript文件包含到我的wordpress插件中。我已经尝试了所有wp_enqeue_script()方法,但没... 查看详情

如何在自定义插件中的 WordPress 自定义表单中添加验证

】如何在自定义插件中的WordPress自定义表单中添加验证【英文标题】:HowtoaddvalidationinWordPresscustomformincustomplugin【发布时间】:2019-03-0911:18:05【问题描述】:我已经制作了一个自定义表单并制作了一个自定义插件来发送电子邮件... 查看详情