Dynamic population editable by post author only

Support MB Frontend Submission Dynamic population editable by post author only

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #10698
    Orice Media SRLOrice Media SRL
    Participant

    How can I achieve dynamic population, which is only editable by the user whom posted it. Do I need to set a specific parameter to metabox, do I use the following code or do I have another hook at my disposal, which I may use for this purpose?

    add_filter( 'rwmb_frontend_field_value_post_id', 'my_custom_population_function', 10, 2 );
    function my_custom_population_function( $value, $args ) {
        if ( $args['id'] === 'your_meta_box_id' ) { // Only filter for a specific form.
            $user_id = get_current_user_id();
            $post = get_post( $value );
            if( $post->post_author != $user_id ){
                // redirect ?!?
            }
        }
        return $value;
    }
    
    #10700
    Orice Media SRLOrice Media SRL
    Participant

    I have tried it with this filter, but the headers are already sent so I can't do a redirect in case the user isn't the author of the post. What hook could I use?

    #10701
    Orice Media SRLOrice Media SRL
    Participant

    I managed to do it the following way.
    Create a page template with the following code inside it.

    <?php
    get_header();
    
    $post_id = isset( $_GET['id'] ) ? absint( $_GET['id'] ) : 0;
    
    if( $post_id ){
        $post = get_post( $post_id );
        if( ( 'your_post_type' == $post->post_type && get_current_user_id() == $post->post_author ) || current_user_can( 'manage_options' ) ){
            echo do_shortcode( '[mb_frontend_form post_id="' . $post_id . '"]' );
        } // endif; is current user
    } // endif; we have a post id
    
    get_footer();

    Hope it helps someone.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘Dynamic population editable by post author only’ is closed to new replies.