Skip Navigation

[Resolved] Change CP Author on Frontend

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to use a CRED generic field to display users, and modify the author of the created post to match the selected user.

Solution: Using a View to create options for a generic field is not a supported feature of Views. The steps other users have had luck with are described below, but not guaranteed to work.

Create a View of all Users:

[wpv-layout-start]
    [wpv-items-found]
    <!-- wpv-loop-start -->
        <wpv-loop>
          [wpv-item index=1]
          {"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}   
          [wpv-item index=other],
          {"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"} 
        </wpv-loop>
    <!-- wpv-loop-end -->
    [/wpv-items-found]
[wpv-layout-end]

Add code to functions.php which will strip extra formatting from the authors View:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
  
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '131' ) { //Please adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if (
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        } else {
            $start = strpos( $out, '>' );
            if ( $start !== false) {
                $out = substr( $out, $start + 1 );
                $end = strpos( $out, '<' );
                $out = trim(substr( $out, 0, $end ));
            }
        }
    }
    return $out;
}

Create a CRED generic field and place the authors View in the options attribute:

[cred_generic_field field='change_post_author' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="All Authors"] ]
}
[/cred_generic_field]

Add cred_save_data hook to functions.php:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==131)
     
{
$my_post = array(
      'ID'           => $post_id,
      'post_author' => $_POST['change_post_author']
  );
    
// Update the post in the database
  wp_update_post( $my_post );
}
}
This support ticket is created 7 years, 3 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by julieP 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#551738

I'd like Administrators on my site to be able to create Custom Posts (CP) on behalf of Contributors on the frontend.

I've looked at this thread which appears to provide a solution https://toolset.com/forums/topic/cred-form-post-author/ however the CRED generic field I created to test it doesn't display on the form. This is what I did:-

I created this View with Content Selection Users, Any Role, I selected not to show the current user and called the View All Authors

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          [wpv-item index=1]
          {"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"}   
          [wpv-item index=other],
          {"value":"[wpv-user field="ID"]","label":"[wpv-user field="display_name"]"} 
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
[wpv-layout-end]

I created a CRED form consisting of just post_title and this to create a drop down select box to show all Users (except the current one) on the site:

[cred_generic_field field='change_post_author' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="All Authors"] ]
}
[/cred_generic_field]

and I added this to my functions file:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==131)
    
{
$my_post = array(
      'ID'           => $post_id,
      'post_author' => $_POST['change_post_author']
  );
   
// Update the post in the database
  wp_update_post( $my_post );
}
}

I added the form to a page but the user drop down (my generic field) isn't displayed, all I see is the post_title field. I tried amending the Views shortcode in the generic field to [wpv-view name="all-authors"] but it made no difference.

I then created a duplicate of my View and called it All Authors 2 and added the following to the same CRED form:-

<select name="change_post_author" data-wpt-type="select" class="wpt-form-select form-select select" id="cred_form_131">[wpv-view name="all-authors-2"]</select>

This successfully creates a dropdown list (and the User selected from the list is assigned as the Author of the post) but it feels like a 'bodge'.

I have two questions for you please:-

1. Why does the generic field not display?
2. Is the way I managed to create the dropdown an acceptable approach?

Many thanks

#551844

1. Why does the generic field not display?
Usually this is because the options list generated by your View is not formatted correctly. Technically, this is an unsupported feature. Views is not designed to be a data provider for itself, it is designed to be a data displayer, and bugs related to this off-label use of Views are not supported. However, I see a critical step other users have implemented that was overlooked in your setup. For example, this other ticket: https://toolset.com/forums/topic/how-use-a-shortcode-instead-of-options-for-cred-forms

You can try to add the following code to functions.php:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );
 
function prefix_clean_view_output( $out, $id ) {
    if ( $id == '12345' ) { //Please adjust to your Views ID
        $start = strpos( $out, '<!-- wpv-loop-start -->' );
        if (
            $start !== false
            && strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-loop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-loop-end -->' );
            $out = substr( $out, 0, $end );
        } else {
            $start = strpos( $out, '>' );
            if ( $start !== false) {
                $out = substr( $out, $start + 1 );
                $end = strpos( $out, '<' );
                $out = trim(substr( $out, 0, $end ));
            }
        }
    }
    return $out;
}

Change '12345' to match your View's numeric ID. This will help format the options appropriately. If this does not work, you can try placing the View that outputs the options somewhere outside the generic field. Place it in the body of your CRED form somewhere, and see if the options list is output correctly. Your values and labels should be accurate, and there should be no extra commas or spaces anywhere.

2. Is the way I managed to create the dropdown an acceptable approach?
I can't recommend this approach, because it's not technically a supported feature and bugs related to this approach will not be actively addressed by our developers. Some other users have had luck with this approach, but it's not guaranteed to work for you, and there are some cases where I know it does not work.

#551998

Christian

Thank you so much for this. Your comments about support fully noted 🙂