1

Im looking for to add categories from a frontend post. I found a thread about this topic How do I create a way for users to assign categories to a post from the frontend? almost 9 years ago. There was a solution that a user created that did exactly what I was looking for but i get the following error.

Is there a better way of doing this any suggestions would be appreciated.

Uncaught Error: __clone method called on non-object in C:\xampp\htdocs\elementor_dev\wp-includes\taxonomy.php:3565 Stack trace:

#0 C:\xampp\htdocs\dev\wp-content\plugins\frontendposter.php\frontend.php(62): update_term_cache(Array)

#1 C:\xampp\htdocs\dev\wp-includes\class-wp-hook.php(287): WPSECrowdCatsClass->process_request('')

#2 C:\xampp\htdocs\dev\wp-includes\class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array)

#3 C:\xampp\htdocs\dev\wp-includes\plugin.php(478): WP_Hook->do_action(Array)

#4 C:\xampp\htdocs\dev\wp-includes\template-loader.php(13): do_action('template_redire...')

#5 C:\xampp\htdocs\dev\wp-blog-header.php(19): require_once('C:\xampp\htdocs...')

#6 C:\xampp\htdocs\dev\index.php(17): require('C:\xampp\htdocs...')

#7 {main} thrown in C:\xampp\htdocs\dev\wp-includes\taxonomy.php on line 3565

I know it has to do with these lines

// Add terms to taxonomy $affected_terms = wp_set_object_terms( $post_id, $suggested_terms, $tax, false ); update_term_cache($affected_terms); return $affected_terms;

5
  • Questions about third party plugins are off-topic, so you might want to reword this. Also, please don't double post.
    – vancoder
    Dec 15, 2020 at 18:01
  • 1
    It looks like the $affected_terms here is being generated with a bad value. Could you try tracing it out e.g. error_log( print_r( $affected_terms, true ) ); and then looking in your error log? Maybe the inputs to wp_set_object_terms too. (it's possible you're actually getting a WP_Error back from wp_set_object_terms, but I'm not sure why that would fail to clone.)
    – Rup
    Dec 15, 2020 at 18:09
  • Thank you. I revised this question and removed the double post
    – Alex Lytle
    Dec 15, 2020 at 18:09
  • And which WordPress version are you using? Although I don't see anything here that looks version-specific or that I know has changed at least.
    – Rup
    Dec 15, 2020 at 18:09
  • Im using the most current version of wordpress. I will try the error log and say if I can find the problem thank you
    – Alex Lytle
    Dec 15, 2020 at 18:11

1 Answer 1

1

just remove this line update_term_cache($affected_terms); and it works perfectly

   <?php 
/*
Plugin Name: WPSE Crowded Cats
Plugin URI: http://wordpress.stackexchange.com/questions/43419/how-do-i-create-a-way-for-users-to-assign-categories-to-a-post-from-the-frontend
Description: Allow visitors to change categories of posts. Ready to use with custom taxonomies and post types. 
Version: 0.1
Author: WPSE
Author URI: http://wordpress.stackexchange.com/users/2110/maugly
License: GPL2
*/


add_action('plugins_loaded','wpse_init_crowd_cats_class');
function wpse_init_crowd_cats_class(){
    new WPSECrowdCatsClass();
}


class WPSECrowdCatsClass { 

    function __construct() {

        // APPEND THE FORM AUTOMATICALLY TO EVERY POST
        add_filter( 'the_content', array( $this,'append_form' ) );

        // TEMPLATE ACTION TAG TO BE USED IN THEME
        // Usage: do_action('wpse_crowd_cats_form');
        // Usage: do_action('wpse_crowd_cats_form', $post_id, $taxonomy );
        add_action( 'wpse_crowd_cats_form', array( $this,'wpse_crowd_cats_form' ), 10, 2 );

        // FORM PROCESSING
        add_action( 'template_redirect', array( $this,'process_request' ) );

    }

    function process_request(){

        // check submission
        if ( ! isset($_POST['crowd-cat-radio']) || ! is_array($_POST['crowd-cat-radio']) )
            return;

        //TODO: check nonce

        // sanitize and check the input
        $suggested_terms = array_map( 'absint', $_POST['crowd-cat-radio'] );
        $post_id = absint( $_POST['crowd-cats-pid'] );
        $tax = $_POST['crowd-cats-tax'];
        if ( ! taxonomy_exists($tax) )
            return;

        // Allow only existing terms. Not sure if this is needed.
        $args = array( 'hide_empty' => false );
        $args = apply_filters( 'mcc_allowed_terms_args', $args, $post_id, $tax );
        $args['fields'] = 'ids';
        $allowed_terms = get_terms( $tax, $args );
        foreach ( $suggested_terms as $key => $term_id )
            if ( ! in_array( $term_id, $allowed_terms ) )
                unset( $suggested_terms[$key] );

        // Add terms to taxonomy
        $affected_terms = wp_set_object_terms( $post_id, $suggested_terms, $tax, false );
       // update_term_cache($affected_terms);
        return $affected_terms;

    }


    function get_form( $post_id=null, $tax='category' ) {

        if ( is_null($post_id) || ! taxonomy_exists($tax) )
            return false;

        $args = array( 'hide_empty' => false );
        $args = apply_filters( 'mcc_get_terms_args', $args, $post_id, $tax );
        $all_terms = get_terms( $tax, $args );

        if ( ! $all_terms )
            return false;

        $post_terms = wp_get_object_terms( $post_id, $tax, array( 'fields' => 'ids' ) );

        $permalink = get_permalink( $post_id );

        $out = "<form id='crowd-cats' action='$permalink' method='POST' >
            <ul >";

        foreach ( $all_terms as $t ) :

            $checked = in_array( $t->term_id, $post_terms) ? 'checked' : '';
            $out .= "<li>
                        <input type='checkbox' id='crowd-cat-$t->term_id' name='crowd-cat-radio[]' value='$t->term_id' $checked /> 
                        <label for='crowd-cat-$t->term_id' >".esc_attr($t->name)."</label>
                     </li>";

        endforeach;

        $out .= "</ul>
                <input type='submit' value='Submit' name='crowd-cats-submit'/>
                <input type='hidden' value='".esc_attr($tax)."' name='crowd-cats-tax'/>
                <input type='hidden' value='$post_id' name='crowd-cats-pid'/>";

        //TODO: set nonce

        $out .= "</form>";

        return $out;

    }



    function append_form($content){

        global $post;

        if ( 'post' != $post->post_type )
            return $content;

        $form = $this->get_form( $post->ID );

        if ( ! $form )
            return $content;

        return "$content \n $form";
    }


    function wpse_crowd_cats_form( $post_id=null, $taxonomy='category' ) {

        if ( is_null($post_id) ) {
            global $post;
            $post_id = $post->ID;
        }

        echo $this->get_form( $post_id, $taxonomy );
    }


} // end of class               
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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