• Resolved june01

    (@june01)


    Hi,
    I am using the form shortcode to enable users to publish pods in frontend. This part is working fine. But I also have the Post Expirator installed which automatically sets an expire date. But it does not work when I publish pods with the form.
    I’ve found this code to programmatically setting up the expirator but I don’t know where to put it.

    So, my question is: Is there a hook that runs immediately after the pods is published and also I would need the pod id for this to work.

Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Thread Starter june01

    (@june01)

    Thanks. Can I also display custom error messages? I want to add some more logic into it but I would need to display error messages in frontend for the user to see in case of failure.
    or maybe block the form when a specific condition is not met.

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @june01

    Depends on what kind of error messages you are talking about.
    All fields have a validation filter available: pods_field_validate_{$type}

    Cheers, Jory

    Thread Starter june01

    (@june01)

    I want to check if the user has already published a post. If that’s the case then an error message should be displayed. Or the form should be blocked with some message at the top indicating that they cannot publish more than one post.

    I think I can figure out how to query the database and see how many published posts the current user has, but I don’t know how to display error messages and where to put the code.

    Plugin Author Jory Hogeveen

    (@keraweb)

    That should be done with PHP code before running the shortcode.
    This can be done within your theme or in a custom plugin, depending on how/where you use it.

    Also, the check would need to be done pre-saving a post, not post: pre_save_pod_item: https://docs.pods.io/code/filter-reference/pods_api_pre_save_pod_item_podname/

    There is a form specific filter as well: pods_shortcode_output
    https://github.com/pods-framework/pods/blob/2.8.0/includes/general.php#L1290-L1300
    With this filter you can change the HTML output of the shortcode.

    Cheers, Jory

    Thread Starter june01

    (@june01)

    thanks a lot. using the pre_save_pod_item I was able to check if the user has published posts and prevent saving in case they have using this code below:

    add_filter('pods_api_pre_save_pod_item_ad_post', 'count_published_pods', 10, 2); 
        function count_published_pods($pieces, $is_new_item) { 
    		
    		 $id = get_current_user_id();
    		if ( $id ) {
    			$params = array(
    				'where' => 't.post_author = "'.$id.'"',
    			);
    			$pods = pods( 'ad_post', $params );
    			if($pods->total() == 0){
    				return $pieces;
    			}
    			 else { 
    				 //if not throw an error. 
    				 wp_die( 'You shall not pass!'); 
    			 } 
    		}
        }

    But I couldn’t figure out how to use pods_shortcode_output. My current code always displays a success message even when the pod is not saved. In the else statement, I would need something to replace it with an error message.

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @june01

    As I mentioned, your check should be done before rendering the shortcode.

    You could also try to fully overwrite the shortcode ouput if a user already has posts. That way the user cannot submit new posts anyway since the form isn’t visible.

    Something like:

    
    add_filter( 'pods_shortcode_output', 'my_pods_shortcode_output' );
    function pods_shortcode_output( $output, $tags, $pod, $context ) {
        if ( 'form' === $context ) {
            // check here if your user already has posts
            if ( true ) {
                // Do not return form if the user already has posts.
                return 'Error message or something else.';
            }
        }
        return $output;
    } 
    
    Thread Starter june01

    (@june01)

    hi,
    the code is not working for me, what I am doing wrong?

    add_filter( 'pods_shortcode_output', 'my_pods_shortcode_output' );
    function my_pods_shortcode_output( $output, $tags, $pod, $context ) {
    	 if ( 'form' === $context ){
    		  // check here if your user already has posts
    		 $id = get_current_user_id();
    		if ( $id ) {
    			$params = array(
    				'where' => 't.post_author = "'.$id.'"',
    			);
    			$pods = pods( 'ad_post', $params );
    			if($pods->total() == 0){
    				 return $output;
    			}
    			 else { 
    				 //if not throw an error. 
    				  return 'Error message or something else.';
    			 } 
    		}
    	 }    	
    }
    • This reply was modified 3 years, 1 month ago by june01.
    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @june01

    You should always return the $output by default (last line of the function).

    add_filter( 'pods_shortcode_output', 'my_pods_shortcode_output' );
    function my_pods_shortcode_output( $output, $tags, $pod, $context ) {
    	if ( 'form' === $context ){
    		// check here if your user already has posts
    		$id = get_current_user_id();
    		if ( $id ) {
    			$params = array(
    				'where' => 't.post_author = "'.$id.'"',
    			);
    			$pods = pods( 'ad_post', $params );
    			if( $pods->total() ){
    				// User already has posts, throw error.
    				return 'Error message or something else.';
    			}
    		}
    	}
    	// Normal output.
        	return $output.
    }
    Thread Starter june01

    (@june01)

    thanks for the replay but the code does still not work. I think it’s not even been called.

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @june01

    Where did you add the code?

    Thread Starter june01

    (@june01)

    I am using code snippets plugin and I’ve added it there.

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @june01

    Sorry for my late reply.
    I’m not sure if this plugin is compatible in adding filters to WordPress. I honestly doubt it. In any case such a plugin is a security right and I would recommend adding such code in your theme’s functions.php file or in a separate plugin.

    Cheers, Jory

    Thread Starter june01

    (@june01)

    hi,
    I added the code in functions.php but same problem, it does not work. Nothing happens.

    Thread Starter june01

    (@june01)

    Hi, I’ve changed the code to solve this issue. Instead of using the form shortcode I am creating my own one, and it’s working. When the user has a published post it displays an alert message instead of the form.

    The only issue I have is on submit the page reloads and then the alert is displayed because the post is saved. But I want a success message too. How do I know if they just opened the page or have successfully submitted the form?

    Also, I would like to change the button text on the form.

    That’s my code

    add_shortcode( 'ad_post_form', 'ad_post_form_shortcode' );
    
    // this function generates the shortcode output
    function ad_post_form_shortcode( $args ) {
    	// check here if your user already has posts
    	$id = get_current_user_id();
    	if ( $id ) {
    		$params = array('where' => 't.post_author = "'.$id.'"',);
    		$pods = pods( 'ad_post', $params );
    		if( $pods->total() > 0){
    			// User already has posts, throw error.
    			$content = '<div class="custom_alert">?? ????? ??? ???? ?? ????? ???? ?? ??? ?????.</div>';
    			return $content;
    		}
    	}
    	
    	
    	$mypod = pods( 'ad_post' );
    	// Override the options of a specific field
    	$fields = array( 'post_title' => array('label'=>'????? ???????'), 'website','ad_position_field','ad_image' );
    	// Output a form with specific fields
    	echo $mypod->form( $fields );
    	
    }
Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Perform custom action after post is publish using form’ is closed to new replies.