我已经为公司网站开发了一些关于职位公告的功能。有一个自定义的帖子类型,带有专门的字段,还有一个小部件,显示当前职位帖子的摘要。它有5个函数和一个widget类,它们从各种网站等蹒跚地聚集在一起,并被转储到函数中。php。它起作用了。
现在我想把它提取到一个插件,但我的理解很弱。。。我的第一次尝试是将其转储到一个带有标题的php文件中,但当我试图激活它时,这会引发一系列错误。然后,我将所有的函数挂钩都放进插件的激活挂钩中,它不会抛出任何错误,但似乎也不会做任何事情。
<?php
/**
* Plugin Name: My Careers Plugin
* Description: This plugin adds a Careers Post Type and careers summary widget .
* Version: 1.0.0
* Author: Marc Pelletier
*/
// Exit if accessed directly
if ( !defined( \'ABSPATH\' ) ) exit;
function activate_my_careers() {
add_action( \'init\', \'create_career_posttype\');
add_action( \'admin_bar_menu\', \'add_careers_to_admin_bar\', 1000000);
add_action( \'add_meta_boxes\', \'add_careers_meta_box\' );
add_action( \'save_post\', \'save_careers_meta_box\' );
register_widget(\'CurrentJobs_Widget\');
}
register_activation_hook( __FILE__, \'activate_my_careers\' );
function create_career_posttype() {
$args = array(
\'labels\' => array(
\'name\' => __(\'Careers\'),
\'singular_name\' => __(\'Careers\'),
\'all_items\' => __(\'All Job Postings\'),
\'add_new_item\' => __(\'Add New Job Posting\'),
\'edit_item\' => __(\'Edit Job Posting\'),
\'view_item\' => __(\'View Job Posting\')
),
\'public\' => true,
\'has_archive\' => true,
\'rewrite\' => array(\'slug\' => \'careers\'),
\'show_ui\' => true,
\'show_in_menu\' => true,
\'show_in_nav_menus\' => true,
\'show_in_admin_bar\' => true,
\'capability_type\' => \'post\',
\'supports\' => array(\'title\', \'editor\', \'thumbnail\'),
\'exclude_from_search\' => true,
\'menu_position\' => 6,
\'has_archive\' => true,
\'menu_icon\' => \'dashicons-universal-access\'
);
register_post_type(\'careers\', $args);
}
function add_careers_to_admin_bar($wp_admin_bar){
$wp_admin_bar->add_node(
array(
\'title\' => \'<span class="dashicons-before dashicons-universal-access" >Careers</span>\'
)
);
$wp_admin_bar->add_node(
array(
\'parent\' => \'careers\',
\'title\' => \'All Job Postings\',
\'href\' => get_admin_url().\'edit.php?post_type=careers\'
)
);
$wp_admin_bar->add_node(
array(
\'parent\' => \'careers\',
\'title\' => \'New Job Posting\',
\'href\' => get_admin_url().\'post-new.php?post_type=careers\'
)
);
}
function add_careers_meta_box(){
add_meta_box( \'job-details\', \'Job Details\', \'careers_meta_box_cb\', \'Careers\', \'normal\', \'default\');
}
function careers_meta_box_cb($post){
$Post_ID = get_post_custom( $post->ID );
$salary = isset( $Post_ID[\'salary\'] ) ? esc_attr( $Post_ID[\'salary\'][0] ) : "";
$salary_unit = isset( $Post_ID[\'salary_unit\'] ) ? esc_attr( $Post_ID[\'salary_unit\'][0] ) : "hr";
$qualifications = isset( $Post_ID[\'qualifications\'] ) ? esc_attr( $Post_ID[\'qualifications\'][0] ) : "";
//echo $qualifications;
$desirables = isset( $Post_ID[\'desirables\'] ) ? esc_attr( $Post_ID[\'desirables\'][0] ) : "";
//echo $desirables;
wp_nonce_field( \'job_details_nonce_action\', \'job_details_nonce\' );
$html = \'\';
$html .= \'<label class="h2">Salary</label>\';
$html .= \'<input type="number" step="0.05" name="salary" id="salary" style="margin-top:15px; margin-left:9px; margin-bottom:10px;" value="\'. $salary .\'" />\';
$html .= \'<label> / </label>\';
$html .= \'<input type="text" list=salary_units name="salary_unit" id="salary_unit" style="margin-left:9px; margin-top:15px;" value="\'. $salary_unit .\'" /></br>\';
$html .= \'<datalist id=salary_units><option>hr<option>day<option>month<option>annum</datalist>\';
$html .= \'<label class="h2"> <strong>Required </strong>Qualifications (one per line) </label></br>\';
$html .= \'<textarea name="qualifications" id="qualifications" cols="80" rows="5" style="margin-left:9px; margin-top:15px";>\'.$qualifications.\'</textarea></br>\';
$html .= \'<label class="h2">Additional Qualifications (one per line) </label></br>\';
$html .= \'<textarea name="desirables" id="desirables" cols="80" rows="5" style="margin-left:9px; margin-top:15px";>\'.$desirables.\'</textarea></br>\';
echo $html;
}
function save_careers_meta_box($post_id){
// Bail if we\'re doing an auto save
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
// if our nonce isn\'t there, or we can\'t verify it, bail
if( !isset( $_POST[\'job_details_nonce\'] ) || !wp_verify_nonce( $_POST[\'job_details_nonce\'], \'job_details_nonce_action\' ) ) return;
// if our current user can\'t edit this post, bail
if( !current_user_can( \'edit_post\' ) ) return;
if(isset( $_POST[\'salary\'] ) )
update_post_meta( $post_id, \'salary\', $_POST[\'salary\']);
if(isset( $_POST[\'salary_unit\'] ) )
update_post_meta( $post_id, \'salary_unit\', $_POST[\'salary_unit\']);
if(isset( $_POST[\'qualifications\'] ) )
update_post_meta( $post_id, \'qualifications\', $_POST[\'qualifications\']);
if(isset( $_POST[\'desirables\'] ) )
update_post_meta( $post_id, \'desirables\', $_POST[\'desirables\']);
}
class CurrentJobs_Widget extends WP_Widget{
function __construct() {
parent::__construct(
\'currentjobs_widget\', // Base ID
\'Current Jobs Widget\', // Name
array(\'description\' => __( \'Displays your latest listings. Outputs the title and expiry per listing\'))
);
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance[\'title\'] = strip_tags($new_instance[\'title\']);
$instance[\'numberOfListings\'] = strip_tags($new_instance[\'numberOfListings\']);
return $instance;
}
function form($instance) {
if( $instance) {
$title = esc_attr($instance[\'title\']);
$numberOfListings = esc_attr($instance[\'numberOfListings\']);
} else {
$title = \'\';
$numberOfListings = \'\';
}
?>
<p>
<label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title\', \'currentjobs_widget\'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id(\'numberOfListings\'); ?>"><?php _e(\'Number of Listings:\', \'currentjobs_widget\'); ?></label>
<select id="<?php echo $this->get_field_id(\'numberOfListings\'); ?>" name="<?php echo $this->get_field_name(\'numberOfListings\'); ?>">
<?php for($x=1;$x<=10;$x++): ?>
<option <?php echo $x == $numberOfListings ? \'selected="selected"\' : \'\';?> value="<?php echo $x;?>"><?php echo $x; ?></option>
<?php endfor;?>
</select>
</p>
<?php
}
function widget($args, $instance) {
extract( $args );
$title = apply_filters(\'widget_title\', $instance[\'title\']);
$numberOfListings = $instance[\'numberOfListings\'];
echo $before_widget;
if ( $title ) {
echo $before_title . $title . $after_title;
}
$this->getJobListings($numberOfListings);
echo $after_widget;
}
function getJobListings($numberOfListings) { //html
global $post;
$listings = new WP_Query();
$listings->query(\'post_type=careers&posts_per_page=\' . $numberOfListings );
if($listings->found_posts > 0) {
echo \'<ul class="currentjobs_widget">\';
while ($listings->have_posts()) {
$listings->the_post();
$listItem = \'<li>\' . $image;
$listItem .= \'<a href="\' . get_permalink() . \'">\';
$listItem .= get_the_title() . \'</a></br>\';
$listItem .= \'<span>Added \' . get_the_date() . \'</span></li>\';
echo $listItem;
}
echo \'</ul>\';
wp_reset_postdata();
}else{
echo \'<p style="padding:25px;">No postings found</p>\';
}
}
} //end class CurrentJobs_Widget
对进一步阅读的建议很好。
最合适的回答,由SO网友:Nathan Johnson 整理而成
如果您只想让插件工作,只需
代替
register_activation_hook( __FILE__, \'activate_my_careers\' );
使用
add_action( \'plugins_loaded\', \'activate_my_careers\' );
如果你想了解为什么你的新插件不起作用,那么你需要了解WordPress挂钩。我鼓励您仔细阅读
WordPress Plugin API.
插件(非必须使用)可用的第一个挂钩是plugins_loaded
. 当我编写插件时,我在主插件文件中输入的唯一内容就是向激活、停用和plugins\\u加载的挂钩添加操作。
plugins\\u-loaded钩子调用的函数/方法应该通过向其他钩子添加操作和过滤器来加载插件。通过使用钩子,您可以让自己和其他插件在启动之前移除这些钩子。这使得插件可以扩展。
关键是要知道什么时候触发哪些钩子,并将函数/方法添加到特定的钩子中,这些钩子只有在您希望函数/方法可用时才会触发。
因此,在您的示例中,当包含主插件文件时,我们要添加的唯一操作是一个用于加载plugins\\u的挂钩。然后,该挂钩的回调将添加插件工作所需的其他操作。
插件无法工作的原因是,激活挂钩仅在插件激活后立即启动。一旦插件被激活,在重定向到另一个页面之前,唯一调用的另一个钩子就是关闭。从那时起,激活钩子不会启动,因为它不会启动,所以您的任何其他操作也不会启动。