如何将按钮添加到分类表中的自定义列?

时间:2012-01-26 作者:MrValueType

我正在做一个issue-based publishing system 为了我的一个朋友。到目前为止,我已经创建了一个名为“问题”的自定义分类法;在自定义分类法中添加了几个自定义字段(如“问题编号”、“PDF URL”和“是否已发布?”);并使用其他列扩展了分类法管理页面,以显示这些自定义术语数据。

现在,我想实现一种方便的方法来发布(和取消发布)单个问题。我的计划是add an additional custom column to the taxonomy management page which contains "Publish" and "Unpublish" buttons. 因此,我尝试用一个包含隐藏数据字段和可见提交按钮的表单填充自定义列;基本上,我复制了默认术语编辑表单的功能,并将其放在表格单元格中。

问题是,整个分类法管理表被包装在一个名为“posts filter”的表单中,并且forms cannot be nested. 所以我的解决方案不能正常工作。

是否有合适的解决方案将此类按钮添加到自定义列?欢迎提出任何建议!

澄清图片:http://img845.imageshack.us/img845/669/customcol.png

add_filter("manage_issue_custom_column", \'manage_issue_columns\', 10, 3);

// The function that manages the custom columns
function manage_issue_columns($out, $column_name, $issue_id) {
    $issue = get_term($issue_id, \'issue\');
switch ($column_name) {
    case \'issue_pdf_url\':
        $term_meta = get_option( "taxonomy_term_$issue_id" );
        if ( isset( $term_meta["issue_pdf_url"] ) ){
            $out .= $term_meta["issue_pdf_url"]; 
        }   
    break;

    case \'issue_num\':
        $term_meta = get_option( "taxonomy_term_$issue_id" );
        if ( isset( $term_meta["issue_num"] ) ){
            $out .= $term_meta["issue_num"];
        }
    break;

    //This is the column where I need to add buttons
    case \'issue_published\':
        $term_meta = get_option( "taxonomy_term_$issue_id" );
        $term_object = get_term($issue_id, "issue");
        if ( isset( $term_meta["issue_published"] ) && $term_meta["issue_published"] == "yes" ){
            //This is the form which shows the publishing buttons; not working due to being nested in other form
            $out .= \'<form name="edittag" id="edittag" method="post" action="edit-tags.php" class="">  <input type="hidden" name="action" value="editedtag" />  <input type="hidden" name="tag_ID" value="\' . esc_attr($issue_id) . \'" />  <input type="hidden" name="taxonomy" value="issue" />  \' . wp_original_referer_field(true, \'current\') . wp_nonce_field(\'update-tag_\' . $issue_id) . \' <input name="name" id="name" type="hidden" value="\' . $term_object->name . \'" />  <input name="slug" id="slug" type="hidden" value="\' . $term_object->slug .\'" />  <input name="description" id="description" type="hidden" value="\' . $term_object->description .\'" />  <input type="hidden" value="" checked="" id="term_meta[issue_published]" name="term_meta[issue_published]">  <input type="submit" value="Unpublish" class="button-primary" id="submit" name="submit"></form>\';
        } else {
            $out .= \'<form name="edittag" id="edittag" method="post" action="edit-tags.php" class="">  <input type="hidden" name="action" value="editedtag" />  <input type="hidden" name="tag_ID" value="\' . esc_attr($issue_id) . \'" />  <input type="hidden" name="taxonomy" value="issue" />  \' . wp_original_referer_field(true, \'current\') . wp_nonce_field(\'update-tag_\' . $issue_id) . \' <input name="name" id="name" type="hidden" value="\' . $term_object->name . \'" />  <input name="slug" id="slug" type="hidden" value="\' . $term_object->slug .\'" />  <input name="description" id="description" type="hidden" value="\' . $term_object->description .\'" />  <input type="hidden" value="yes" checked="checked" id="term_meta[issue_published]" name="term_meta[issue_published]">  <input type="submit" value="Publish" class="button-primary" id="submit" name="submit"></form>\';
        }
    break;

    default:
    break;
}

return $out;
}

1 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

对于这种情况,您可以考虑添加一个“action”;当您将鼠标悬停在其他项目上时会显示。

下面的代码不完整,还没有经过测试,但应该会让您走上正确的轨道

Add the action

首先,要添加操作链接,请使用tag_row_actions 钩(请记住,与此挂钩的函数将在每个分类法上启动,因此需要检查分类法)。类似于:

add_filter(\'tag_row_actions\',\'my_term_action\',10,2);
function my_term_action($actions,$tag){
if($tag->taxonomy == \'issue\'):
    $actions[\'my-action-class\'] = \'<a href="#"> My action </a>\';
endif;
return $actions;
}
The$actions 数组是关联的,其中键将成为span 包装动作的元素。值是span 将环绕。

您可以使用$tag (这是一个对象,术语id由$tag->term_id) 确定要显示的操作,因此如果要显示“发布”操作

$actions[\'my-action-class\'] = \'<a class="Publish" termid="\'.$tag->term_id.\'" href="#"> Publish </a>\';
如果要显示“取消发布”操作,请添加:

$actions[\'my-action-class\'] = \'<a class="Unpublish" termid="\'.$tag->term_id.\'" href="#"> Unpublish </a>\';
我给他们不同的类和一个额外的属性。jQuery/AJAX将使用它执行相关操作。。。

The jQuery /AJAX

jQuery(document).ready(function(){
    jQuery(".my-action-class a").click(function(){
        change_publish_states(jQuery(this));
    });
});
    function change_publish_states(el){
   if(el.hasClass(\'Publish\')){
      new_state = \'Unpublish\';
   }else{
      new_state = \'Publish\';
  }
        jQuery.getJSON(ajaxurl,
            {   term_id: el.attr("termid"),
                action: "my_change_publish_state",
                new_state: new_state
            },
            function(data) {
                if (data.error){
                    alert(data.error);                      
                }else{
                    //Update label and class
                    el.text(data.new_state);
                    el.attr(\'class\', data.new_state)
                }
            });
    }
您需要在“自定义分类法”页面上添加此脚本,您可以在上运行函数admin_enqueue_scripts 检查正在查看的页面并有条件地将其排队;

add_action(\'admin_enqueue_scripts\',\'load_my_ajax_script\');
function load_my_ajax_script(){
 //Check globals like $pagenow==\'edit-tags\' and $taxonomy==\'issues\' or $current_screen 
 //to make sure you are on the right page and enqueue script - or add a filter 
 //to print the script at the footer.

 global $current_screen;
 if ( $current_screen->id != \'edit-issue\' )
  return;

 wp_enqueue_script( \'my_ajax_save\', get_stylesheet_directory_URI() . \'/my_ajax_save.js\' );
}
然后还要在AJAX操作挂钩上添加一个函数:

AJAX action

add_action(\'wp_ajax_my_change_publish_state\', \'change_the_publish_state\');
function change_the_publish_state(){

   $term_id = $_GET[\'term_id\'];
   $new_state = $_GET[\'new_state\'];

   //Perform checks / logic & update publish status.
   $term_meta = get_option( "taxonomy_term_$term_id" );

   if ( $new_state == "Unpublish" )
    $term_meta[\'issue_published\'] = \'yes\';

   if ( $new_state == "Publish" )
    $term_meta[\'issue_published\'] = \'\';

   update_option( "taxonomy_term_$term_id", $term_meta );    

//return the new state of the term
   $result[\'new_state\'] = $new_state;
    echo json_encode($result);
    exit();
}

结束

相关推荐

在WP-Admin中修改“最近评论”列表

这可能有点棘手,但我正在寻找一种方法,使wp admin上的最近评论小部件更有用/更受操作驱动。我希望能够过滤,只显示悬而未决的评论(因为这些需要对他们采取行动)。我在第45行看到wp-admin/includes/dashboard.php 有:// Recent Comments Widget if ( is_blog_admin() && current_user_can(\'moderate_comments\') ) { if ( !iss