alterfw/wordpress-for-developers

一个用于在Wordpress中创建CRUD元素的库

v0.1.4 2015-09-13 21:23 UTC

This package is auto-updated.

Last update: 2024-09-21 22:05:12 UTC


README

该库的目的是简化在wordpress中创建CRUD元素,例如元框、在术语和选项页面中的额外字段。

加载库

如果您将库放在wp-content目录中,您可以通过以下方式加载它

<?php
include_once(WP_CONTENT_DIR . '/WD/lib/load.php');

添加元框

您可以使用wd_meta_box()函数创建一个元框

<?php
function register_meta_boxes(){
	wd_meta_box('Extra post options') // meta box name
		->setPage('post') // the post type where the meta box will appear (post, page etc)
		->setContext('side') // side, advanced etc...
		->add(Form::text('my_extra_option')->setLabel('Extra option: ')) // add a field to the meta box
		->add(Form::select('author_box')->setLabel('Author box: ')
				->add('top', 'Show author box in top')
				->add('bottom', 'Show author box in bottom')
		)
		->add(Form::checkbox('is_featured')->setLabel('Post featured? '))
		->init(); // init the necessary hooks
}
add_action('admin_init', 'register_meta_boxes');

查看UI的外观

Meta box

您可以通过以下方式获取帖子的信息

<?php
// get
wd_mb()->getAuthorBox($postID); // or wd_mb()->get('author_box', $postID);

// prints
wd_mb()->theAuthorBox($postID); // or wd_mb()->the('author_box', $postID);

// check if is empty
wd_mb()->isMyExtraOptionEmpty($postID); // or wd_mb()->isEmpty('my_extra_option', $postID);

// example:
while(have_posts()): the_post()
	$authorBoxPosition = wd_mb()->getAuthorBox(); // $postID is not necessary in this case
	
	if($authorBoxPosition == 'top')
		call_some_function_that_display_the_author_box();
		
	echo 'Title: '; the_title(); // post title
	
	if($authorBoxPosition == 'bottom')
		call_some_function_that_display_the_author_box();
	
	if(!wd_mb()->isMyExtraOptionEmpty()){
		echo 'My extra option: '; wd_mb()->theMyExtraOption(); // prints
	}
endwhile;

添加额外的分类字段

<?php
wd_taxonomy('category') // which taxonomy you wanna affect
	->add(Form::textarea('extra_info')->setLabel('Extra information: ')) // add field
	->add(Form::text('order')->setLabel('Order')) // another
	->init();

查看UI的外观

Meta box

从术语获取数据

<?php
// get
wd_tax()->getOrder($termID); // or wd_tax()->get('order', $termID);

// prints
wd_tax()->theAuthorBox($termID); // or wd_tax()->the('order', $termID);

// check if is empty
wd_tax()->isExtraInfoEmpty($termID); // or wd_tax()->isEmpty('extra_info', $termID);

创建管理菜单

<?php
function register_admin_pages(){
	wd_page('Site options') // Page title
		->add(Form::text('video_limit')->setLabel('Video pages show at most: '))
		->init();
		
	wd_page('Social Network', 'site-options') // this page is child of Site options ("site-options" is the slug of "Site options")
		->add(Form::text('facebook_link')->setLabel('Facebook: '))
		->add(Form::text('twitter_link')->setLabel('Twitter: '))
		->add(Form::text('flickr_link')->setLabel('Flickr: '))
		->add(Form::text('youtube_link')->setLabel('Youtube: '))
		->init();
}
add_action('_admin_menu', 'register_admin_pages'); // the hook _admin_menu happens before the admin_menu hook

查看UI的外观

Meta box

从页面选项获取数据

<?php
// get
wd_opt()->getVideoLimit(); // or wd_opt()->get('video_limit');

// prints
wd_opt()->theVideoLimit(); // or wd_opt()->the('video_limit');

// check if is empty
wd_opt()->isYoutubeLinkEmpty(); // or wd_opt()->isEmpty('youtube_link');