Skip Navigation

[Resolved] Function Code with conditional not working for some reason

This support ticket is created 4 years, 2 months 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: Africa/Casablanca (GMT+01:00)

This topic contains 5 replies, has 2 voices.

Last updated by Jamal 4 years, 2 months ago.

Assisted by: Jamal.

Author
Posts
#1843877

Tell us what you are trying to do?
I'm trying to get this custom code with the "if" statement to work. It's not for some odd reason. It works when I remove the conditional code and create a new "Download" custom post type.
Is there any documentation that you are following?
https://toolset.com/forums/topic/bbpress-post-on-cred-submit/
Is there a similar example that we can see?
NA
What is the link to your site?
beta.iwebnow.net

This is my current code:

//**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 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);
}
}
}

#1844165

Hello and thank you for contacting the Toolset support.

Make sure that the custom post type has a checkbox custom field with slug "has-support-topic", otherwise this line will not return an actual value:

$has_support_topic = get_post_meta($post_id, 'wpcf-has-support-topic', true);

You can also use erro_log and print_r PHP functions to inspect the variables in the code for debugging.
- hidden link
- hidden link

#1844509

Hi Jamal,
The slug in the check box is indeed "has-support-topic" but for some reason it's not posting the file when I add the if statement. I'm not really sure. I pulled the debug log and this is what it says

[14-Nov-2020 19:29:20 UTC] PHP Notice:  Undefined variable: topic_data in functions.php on line 80
[14-Nov-2020 19:29:20 UTC] PHP Notice:  Undefined variable: topic_meta in functions.php on line 93
[14-Nov-2020 19:29:20 UTC] PHP Notice:  Undefined variable: topic_id in functions.php on line 96
[14-Nov-2020 19:29:20 UTC] PHP Notice:  Undefined variable: topic_id in functions.php on line 101
[14-Nov-2020 19:29:20 UTC] PHP Notice:  Undefined variable: topic_id in functions.php on line 102

Here is my code:

 
//**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
		$title = get_the_title( $post_id );
		$content = get_post_meta($post_id, 'wpcf-about-this-file', true);

        // 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'      => $content,
        'post_title'        => $title,
		'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);
}    
	  
}
}
//**BBPRess Post Function End **//
#1845581

The PHP notices are expected because, in lines 18 and 31 (from the code on your previous reply), $topic_data and $topic_meta are not defined, but you use them in the first argument of bbp_parse_args, you can avoid these notices if you make those variable empty arrays, example:

    $topic_data = array();
    $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'      => $content,
        'post_title'        => $title,
        'comment_status' => 'open',
        'menu_order'     => 0,
    ), 'insert_topic' );

$topic_id is used in lines 34, 39, and 40, but it is not defined.

Can you do an error_log($has_support_topic); after line 43 and check what it will write on the debug.log file?

#1846331

This is the result I get in the error log now after doing as you requested

[16-Nov-2020 17:14:55 UTC] PHP Notice:  Undefined variable: has_support_topic in /functions.php on line 90
[16-Nov-2020 17:14:55 UTC] 
[16-Nov-2020 17:14:55 UTC] PHP Notice:  Undefined variable: topic_id in functions.php on line 96
[16-Nov-2020 17:14:55 UTC] PHP Notice:  Undefined variable: topic_id in functions.php on line 101
[16-Nov-2020 17:14:55 UTC] PHP Notice:  Undefined variable: topic_id in functions.php on line 102
#1847069

I think that you tried to error_log( $has_support_topic ) before you put a value in it. Place it after line 43 as follow:

$has_support_topic = get_post_meta($post_id, 'wpcf-has-support-topic', true);
error_log($has_support_topic);
if ( $has_support_topic == 1 ){
    // Now create the topic
    $topic_id = bbp_insert_topic($topic_data, $topic_meta);
}

Please consider that this is custom code and beyond the scope of this support forum https://toolset.com/toolset-support-policy/
You may want to ask your question in a generic WordPress development site such as StackExchange:
- https://wordpress.stackexchange.com/
- https://stackoverflow.com/questions/tagged/wordpress