Skip Navigation

[Resolved] Adding function/filter to add bbpress topic on admin post_save action

This support ticket is created 4 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 7 replies, has 2 voices.

Last updated by Waqar 4 years, 1 month ago.

Assisted by: Waqar.

Author
Posts
#1842639

Tell us what you are trying to do?
I'm trying to work with previous code that Jamal helped me with to add a action/filter to get it to post a bbpress topic if the admin/user updates the post in the dashboard and clicks on "update" with a check box marked "yes".
Is there any documentation that you are following?
N/A
Is there a similar example that we can see?
N/A
What is the link to your site?
beta.iwebnow.net

The current code I"m using is

//**New BBPress Topic When CRED Submit **// 
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action ($post_id, $form_data)
{
	//set the forum ID that the topic will be saved to
	$forum_ID = '3967' ;
	
	if (!function_exists('bbp_insert_topic')) {
            require_once '/includes/topics/functions.php';
        }
	
  // change this 12 to the actual id of your form
  if ( $form_data['id'] == 222) {
		//getting post
		$post_form = get_post($entry['post_id']) ;
		$title = $post_form->post_title;
		$content = $post_form->post_content;

        // The topic data.
        $topic_data = bbp_parse_args( $topic_data, array(
		'post_parent'    => $forum_ID, // forum ID
		'post_status'    => bbp_get_public_status_id(),
		'post_type'      => bbp_get_topic_post_type(),
		'post_author'    => bbp_get_current_user_id(),
		'post_password'  => '',
        'post_content'      => get_post_meta($post_id, 'wpcf-about-this-file', true),
        'post_title'        => get_post_meta($post_id, 'wpcf-file-name', true),
		'comment_status' => 'open',
		'menu_order'     => 0,
	), 'insert_topic' );
    
     // The topic meta.
	$topic_meta = bbp_parse_args( $topic_meta, array(
		'author_ip'          => bbp_current_author_ip(),
		'forum_id'           => $forum_ID,
		'topic_id'           => $topic_id,
		'voice_count'        => 1,
		'reply_count'        => 0,
		'reply_count_hidden' => 0,
		'last_reply_id'      => 0,
		'last_active_id'     => $topic_id,
		'last_active_time'   => get_post_field( 'post_date', $topic_id, 'db' ),
	), 'insert_topic_meta' );
$has_support_topic = get_post_meta($post_id, 'wpcf-has-support-topic', true);
if ( $has_support_topic == 1 ){
  // Now create the topic
  $post_id = bbp_insert_topic($topic_data, $topic_meta);
}
}
}
#1842673

I tried to make a new action then copy the above code into it, but it doesn't trigger on post_save in the dashboard if the checkbox is checked. So I'm not understanding how it triggers and where the data comes from in the case of updating a post.

add_action('post_save','my_save_post_data_action',10,2);
function my_save_post_data_action ($post_id, $form_data)
#1843001

Hi,

Thank you for contacting us and I'd be happy to assist.

If your goal is to execute some custom code whenever a post is added or edited through the admin area, the WordPress hook that you're looking for is "save_post" and not "post_save":
https://developer.wordpress.org/reference/hooks/save_post/

You'll find some useful details and examples in this guide on how to use this hook to avoid infinite loops and to only trigger the custom function for the specific post types.

Tip: From more information section, you'll also note that you'll be getting the data from "$_POST" instead of the "$form_data" that is available when using the front-end form and hook "cred_save_data".

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1843037

Hi Waqar,
Thanks for the feedback.
So the code above is working fine when I actually submit a new custom post type "Downloads" into my database. It does create the bbpress topic in the appropriate forum but only if I check the checkbox "has support". If I don't check the check box, then no topic is created. So for some of my downloads, I don't need a support topic when I create the custom post. However; later I might need to create a topic. So, when I update the post and click the check box, I need it to create the bbpress topic. I'm kind of stuck on how to accomplish this. I can create a front end edit form using CRED. Would it be easier to do it that way vs the save_post ?

#1845481

Thanks for writing back.

I can test this on my website and suggest something accordingly, but first, I'll need to see how this "Downloads" post type and the checkbox field is set up.

Can you please share the temporary admin login details for your website?

Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

#1848387

Thank you for sharing the admin access.

I've performed some tests on my website with a similar checkboxes type custom field and here are my findings.

In the code snippet that you've shared few messages back, you're using "get_post_meta" ( get_post_meta($post_id, 'wpcf-has-support-topic', true) ) function to get the value in the "$has_support_topic" variable and then you use it in a condition to check if this value is equal to 1.

But as Toolset Types stores checkboxes type custom field values in a special format in the database, that value is never equal to 1 and hence that condition can never be true.

To fix this, you can use the "types_render_field" function from types to get the checkboxes type custom field value:
https://toolset.com/documentation/customizing-sites-using-php/functions/#checkboxes

Here is an example of a function hooked to a front-end form that creates a new download post:


add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action ($post_id, $form_data)
{   
	// change this 1234 to the actual id of your form
	if ( $form_data['id'] == 1234) {
		$has_support_topic = types_render_field( "has-support-topic", array( "item" => $post_id ) );
		
		if ( $has_support_topic == 1 ){
			// Now create the topic
			
		}
	}
}

Note: Please replace 1234 with your actual form's ID and include the code for the BBpress topic creation as needed.

Similarly, if you'd like to also target the back-end downloads post edit screen using the "save_post" hook, the function would look like this:


function my_save_post_downloads_action( $post_id ) {
 
	// If this is just a revision, stop.
	if ( wp_is_post_revision( $post_id ) ) {
		return;
	}

	if ( isset($_POST['wpcf']['has-support-topic']) && (!empty($_POST['wpcf']['has-support-topic'])) ){
		// Now create the topic
		
	}
}
add_action( 'save_post_downloads', 'my_save_post_downloads_action' );

Important note: Your logic of using the "has-support-topic" field to insert a new topic from the downloads post automatically should be fine for the case of a front-end form that creates a new download post, as the function attached to it will only execute once (i.e. at the time of download post creation ).

However, if you'll later link a similar function to a front-end form that edits a download post or use a function hooked to a back-end downloads post edit screen using the "save_post" hook, you'll end up with multiple support topics created for a single download post every time post is edited with "has-support-topic" field checked.

To avoid that, you can include a new single-line field for example "created-support-topic" along with the "has-support-topic" field in which the ID of the automatically created topic can be saved. This way, you'll be able to track that a support topic has been created for this particular download already and it shouldn't be created again.

I hope this makes sense.

#1849413

Hi Waqar,
Thanks so much for helping with this. I had tested this with the renders field but wasn't sure if it was right to mix the api's.

As far as the editing goes, I think I've just decided to create a CRED edit form and only edit the post types through this vs the backend. But if I understand what you've said correctly, I will still need to use the save_post_downloads add_action?
So by using this action, I would need to automatically have the function fill out the newly created link to the download. I believe I can use the

bbp_get_topic_permalink( $topic_id  );

but I'm a little unsure how I can get the url to return into the CRED field "has-support-url". Could I just create a variable and return the permalink value and somehow user an array to fill out the field after the topic has been created?

Thanks again for your help with this, it's really helping me to understand how to code functions with wordpress and toolset overall.

#1849677

Thanks for the update and glad that my message helped.

> As far as the editing goes, I think I've just decided to create a CRED edit form and only edit the post types through this vs the backend. But if I understand what you've said correctly, I will still need to use the save_post_downloads add_action?

- That is fine and if you'll only be using the front-end Toolset forms for adding and editing Downloads posts, then you can ignore the "my_save_post_downloads_action" function.

If you're going to follow my recommendation about saving the automatically created topic in a new custom field like "has-support-topic", I'll recommend to store the ID and not the URL as URLs can change, but ID stays constant and unique and if you have an ID of the item, you can extract any information from it, including the URL.

From your code snippet, I believe that "$post_id = bbp_insert_topic($topic_data, $topic_meta);" line should give you that newly created topic ID in "$post_id" variable.
( but you should change $post_id to $topic_id in that line since $post_id would already hold the created/edited "Download" post's ID and you don't want to overwrite that )

Once you have the topic ID, you can use the "update_post_meta" function to store it in your desired custom field:
https://developer.wordpress.org/reference/functions/update_post_meta/

Tip: For step-by-step custom code troubleshooting and data dumping, you'll find the "WP PHP Console" plugin ( https://wordpress.org/plugins/wp-php-console/ ) very useful.