Hook into create_category

时间:2013-10-16 作者:Abris

我正试图以某种方式在创建一个新类别时与之挂钩。我想做的是查看创建的术语和slug,并记录它们/通过电子邮件发送它们。我已尝试连接以创建\\u category()。这只给了我一个ID,而不是术语和slug。如果有人能引导我走上正确的道路,那就太好了。

2 个回复
最合适的回答,由SO网友:nathanaelphilip 整理而成

可以使用get\\u term\\u by()函数:http://codex.wordpress.org/Function_Reference/get_term_by

// get the category object
$category = get_term_by( \'id\', $category_id, \'category\');

// use the object to get your values
$name = $category->name;
$slug = $category->slug;
这有帮助吗?

SO网友:Ajay singh
<?php
 /*
  Plugin Name: Xml Write
  Plugin URI: http://wordpress.org/plugins/
  Description: This is post category xml write plugin
  Version: 1.6
  Author URI:
 */

add_action(\'admin_menu\', \'xmlwrite_admin_actions\');

function xmlwrite_admin_actions() {
    add_options_page(\'xmlwrite\', \'xmlwrite\', \'manage_options\', xmlwrite, \'xmlwrite_admin\');
}

add_action(\'add_category_form_pre\', \'update_category_function\');
add_action(\'edit_category\', \'update_category_function\');
add_action(\'delete_category\', \'update_category_function\');

function update_category_function($category_id) {
    $categories = get_categories($category_id);

    $xml = new XMLWriter();
    $xml->openURI("text.xml");
    $xml->startDocument();
    $xml->setIndent(true);

    $xml->startElement(\'Categories\');

    foreach ($categories as $category) {

        $xml->startElement("Category");
        $xml->writeRaw($category->name);
        $xml->endElement();

        $xml->startElement("url");
        $xml->writeRaw(esc_url(get_category_link($category->term_id)));
        $xml->endElement();
    }
    $xml->endElement();

    header(\'Content-type: tet/xml\');
    $xml->flush();
}
结束