0

i'm trying to save a custom term with my own informations.

i tried the hooks: - create_term - created_term - create_{my_taxonomy}

but nothing seems to work.

when I save the term, it's only getting saved with the default wordpress informations. When I then edit the term, the hook - edit_term works finde!

actually I have no idea, why the create_term is not fired. is there anything else to do or any other hook. or is it a problem, that wordpress saves the term by ajax?

here a code snipped of how i use the hook

if ( ! defined ( 'ABSPATH' ) ) {
    exit;
}

class my_term_class {
    public function __construct() {
        add_action( 'create_term', array( $this, 'save_my_fields' ), 10, 3 ); 
        add_action( 'edit_term', array( $this, 'save_my_fields' ), 10, 3 );
        //...
    }

    public function save_my_fields( $term_id, $tt_id = '', $taxonomy = '' ) {
        write_log("this log is only written by edit_term!");
        //...
    }
}
new my_term_class();

Edit: I created a brand new taxonomie to my post type. but still the same problem.

here the Code I create the taxonomie:

        register_taxonomy("cstesttaxonomy", 
        array('myposttype'),
        array(
            "label"             => _n('Testtaxonomie', 'Testtaxonomies', 2, 'cs-poedit'),
            'public'            => false,
            'labels' => array(
                'name'          => _n('Testtaxonomie', 'Testtaxonomies', 2, 'cs-poedit'),
                'sigular_name'  => _n('Testtaxonomie', 'Testtaxonomies', 1, 'cs-poedit'),
                'menu_name'     => _n('Testtaxonomie', 'Testtaxonomies', 1, 'cs-poedit'),
                'edit_item'     => __('Edit testtax', 'cs-poedit'),
                'view_item'     => __('View testtax', 'cs-poedit'),
                'update_item'   => __('Update testtax', 'cs-poedit'),
                'add_new_item'  => __('Add new testtax', 'cs-poedit'),
                //                     'new_item_name' => 'Optionsbezeichnung',
                'popular_items' => NULL,
            ),
            "hierarchical"      => false,
            "singular_label"    => _n('Testtaxonomie', 'Testtaxonomies', 1, 'cs-poedit'),
            'rewrite'           => true,
            'show_ui'           => true,

            'show_in_nav_menus'  => true,
            'show_admin_column' => true,
        )
    ); 

than I add the administration view for this taxonomie to my admin menu:

private function initAdminHooks() {
    add_action( 'admin_menu', array($this, 'initAdminMenu'));
}

public function initAdminMenu() {
    add_menu_page('MyMenu', 'MyMenu', 'edit_posts', 'cs_mymenu', 'show', plugins_url('myplugin/myicon_16x16.ico'), 3);
    //...
    add_submenu_page('cs_mymenu', _n('TestTax', 'TestTax', 2, 'cs-poedit'),_n('TestTax', 'TestTaxes', 2, 'cs-poedit'), 'manage_options', 'edit-tags.php?taxonomy=cstesttaxonomy&post_type=myposttype', false);
}   

and then in the administration view I want to add a new taxonomie and when the taxonomie is beeing saved, i want to write a log (if that works, i want to add own fields to the taxonomie and save custom values to a custom database table)

and my class to do that and handle the save method is:

if ( ! defined ( 'ABSPATH' ) ) {
    exit;
}

class RS_IB_MyTest {

public function mytest1( $term_id = 0, $tt_id ='') {
    write_log("Stest1");
}
public function mytest2( $term_id, $tt_id ='',$asd = '' ) {
    write_log("Stest2");
}        

public function __construct() {
    write_log("construct");
    add_action( 'create_cstesttaxonomy', array($this, 'mytest1'), 10, 2 );        

    add_action( 'edit_term', array( $this, 'mytest2' ), 10, 3 );
}
}
new RS_IB_MyTest();

but still, only the method from the "edit_term" hook is fired correctly.

6
  • I can not reproduce the issue with your code. Can you post the real code you are using? What are you trying do exactly?
    – cybmeta
    Mar 2 '16 at 12:00
  • I updatet my post. but even with a new taxonomy i'm getting the same problem Mar 2 '16 at 12:29
  • Againg, your code works perfectly here. Can you add the code you are using to register the custom taxonomy?
    – cybmeta
    Mar 2 '16 at 12:41
  • I have added the code for register the taxonomy. see the first code block of my edit --> register_taxonomy("cstesttaxonomy", ..... or have I missunderstand your question? Mar 2 '16 at 12:59
  • I see your register_taxonomy() but not the hook where you execute it. Are you using init action to register the taxonomy?
    – cybmeta
    Mar 2 '16 at 13:51
1

I found my failure... a few months ago I added the following line to my code:

if(!(defined('DOING_AJAX') && DOING_AJAX)) {
    include_once 'mytestfile.php';
}

in this file, I add my action for creating a term... with this line, i wanted to prevent to load some files that I doesn't need when I'm using AJAX... but wordpress is using AJAX for create a new term..

now I removed this if-statement and everything works well...

sorry that I stolen your time for this stupid failure.. but maybe someone has the same problem and is happy about my post :-D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.