Hi,
I want users on my site to be able to write blog posts but I want the new posts to be assigned to a specific category based on a user field.
For example the user has a User Field called Team and it is selected as Sweden 2018.
When that user writes a blog post using a Cred Post Submission form I want that post to be assigned to the category Blog and subcategory Team Sweden 2018
Right now the problem is that users can see and select all the categories on the Cred form and they are choosing the wrong categories and I have to go around fixing their blog categories.
Hi, you can use the cred_save_data hook to programmatically set specific terms on a post created by Forms. First, edit the Form and remove the taxonomy input fields so the Users can't make any term selections. Then use this code template to specify which term(s) correspond to each possible User field value and apply those terms to the new post:
add_action('cred_save_data', 'set_blog_terms',100,2);
function set_blog_terms($post_id, $form_data) {
global $current_user;
$forms = array( 999 );
if (in_array($form_data['id'], $forms))
{
$array = [
'123' => array('blog','team-sweden-2018'),
'456' => array('blog', 'team-norway-2017'),
];
$taxonomy = 'category';
$userFieldValue = get_user_meta( $current_user->ID, 'wpcf-userfieldslug', true);
$tag = array( $array[$userFieldValue] );
wp_set_object_terms( $post_id, $tag, $taxonomy, true );
}
}
Replace 999 with the numeric ID of this Form, or a comma-separated list of Form IDs you want to include in this filter. Then in the $array section, you will create one line for each possible value in the User field. For example, let's say it's a select field, and one option is "Team Sweden" with the content or value team-sweden. Replace 123 with team-sweden, and replace 'blog', 'team-sweden-2018' with a comma-separated list of term slugs you would like to assign to the new post. Surround each term slug in single quotes. Replace userfieldslug with the slug of your User field.
This may need to be adjusted slightly depending on your setup, so I'll need more information if you need additional assistance.
Hi,
Thanks for the code.
I tested the blog writing form with a test user. I was able to write a blog post but the wrong category was selected for the post.
The user field is a Radio select and it only has two possible selections at the moment (malawi-2018 and oma-kapua-nepal-2018).
This is what I added this to my child themes functions.php.
add_action('cred_save_data', 'set_blog_terms',100,2);
function set_blog_terms($post_id, $form_data) {
global $current_user;
$forms = array( 9544 );
if (in_array($form_data['id'], $forms))
{
$array = [
'malawi-2018' => array('blogi','kapua-2018-malawi'),
'oma-kapua-nepal-2018' => array('blogi','oma-kapua-2018-nepal'),
];
$taxonomy = 'category';
$userFieldValue = get_user_meta( $current_user->ID, 'wpcf-kapuaja-hanke', true);
$tag = array( $array[$userFieldValue] );
wp_set_object_terms( $post_id, $tag, $taxonomy, true );
}
}
I attached a screenshot of the user field settings.
Is the user fields Radio select field the culprit?
This code looks okay to me, and the radio field seems to be set up correctly. May I log in and see how this is set up in wp-admin? Also please tell me which User account you used to test the Form, and where I can find the Form on the front-end of the site.
Okay here's an update that should resolve the syntax problems:
add_action('cred_save_data', 'set_blog_terms',100,2);
function set_blog_terms($post_id, $form_data) {
global $current_user;
$forms = array( 9544 );
if (in_array($form_data['id'], $forms))
{
$array = [
'malawi-2018' => array('blogi','kapua-2018-malawi'),
'oma-kapua-nepal-2018' => array('blogi','oma-kapua-2018-nepal'),
];
$taxonomy = 'category';
$userFieldValue = get_user_meta( $current_user->ID, 'wpcf-kapuaja-hanke', true);
if(!$userFieldValue) {
return;
}
$tag = $array[$userFieldValue];
wp_set_object_terms( $post_id, $tag, $taxonomy, true );
}
}
Dreamweaver also gave me an error and said the code had syntax errors on these lines:
In the code sample you provided, it looks like the forum changed some of the brackets to HTML entities somehow, they show up as the entities 091 and 093. Check the code I provided above and compare it to the code in your sample. The syntax works fine for me, can you tell me what version of PHP you are running?
Hi,
Thanks for the quick reply.
I'm running PHP version 5.6.27
I will try the code you posted tomorrow and give you an update.
Sure, if it continues to complain you can try this variation:
$array = array(
'malawi-2018' => array('blogi','kapua-2018-malawi'),
'oma-kapua-nepal-2018' => array('blogi','oma-kapua-2018-nepal'),
);
Hi Christian,
Sorry for the delay.
I inserted the code to my child theme's functions.php and it works beautifully now.
When users with a certain user field write blog posts using the Cred form their posts get the correct category assigned to them automatically.
This is a really valuable feature and saves me a lot of trouble and makes it easy for the users to write blog posts.
Thank you!