Here is a revision that assumes your usuario
field on the edicin
post type is specified as “Multiple Select” under “Relationship Options” under “Edit Field”. I expect this may be the case, as associating multiple users with the post would require this.
The code below takes several things into consideration:
- The first function adds the form to the end of all the
edicin
posts automatically. It is noted in the comments how to add to a PHP template instead, if that’s preferable.
- The second function outputs a save button. If the user is not logged in, it provides a link to login that will redirect back to the post. If the user is already associated with the post, the button will remove from the relationship. If the user is not associated with the post, the button will add the user to the relationship. A _wpnonce field is also included, which is a way of verifying that the user actually clicked the button.
- The third function processes the form. It is on ‘template_redirect’, which only runs on frontend templates, as apposed to ‘init’, which loads everywhere. It accounts for either possible setting you might have under
Pods Admin > Settings > Performance > Watch WP Metadata Calls
. It either adds the user to the relationship or removes them.
Here is the example code:
<?php
/**
* Ejemplo de agregar el botón al final del contenido para cada publicación de 'edición'.
*
* Esto también podría agregarse a la plantilla PHP:
* echo apply_filters( 'edicin_save_button', '' );
*/
add_filter(
'the_content',
function ( $content ) {
if ( 'edicin' !== get_post_type() ) {
return $content;
}
return sprintf(
'%s%s',
$content,
apply_filters( 'edicin_save_button', '' )
);
}
);
/**
* Mostrar un botón para asociar la 'edición' actual con un usuario.
*
* Uso en una plantilla:
* echo apply_filters( 'edicin_save_button', '' );
*/
add_filter(
'edicin_save_button',
function( $default ) {
if ( 'edicin' !== get_post_type() ) {
return '';
}
if ( ! is_user_logged_in() ) {
return sprintf(
'<p class="notice">%s</p>',
sprintf(
__( 'Por favor, <a href="%s">inicia sesión</a> para guardar en tu colección.', 'edicin' ),
esc_url( wp_login_url( get_the_permalink() ) )
)
);
}
/**
* Esto supone que el campo 'usuario' es una relación "multi-select".
*/
$edicin_pod = pods( 'edicin', get_the_ID() );
$associated_user_ids = (array) $edicin_pod->field('usuario', false );
/**
* This is necessary if "Pods Admin > Settings > Performance > Watch WP Metadata Calls" is set to:
* "Enable watching WP Metadata calls (may reduce performance with large processes)"
*/
if ( isset( $associated_user_ids[0] ) && is_array( $associated_user_ids[0] ) ) {
$associated_user_ids = wp_list_pluck( $associated_user_ids, 'ID' );
}
$is_in_user_collection = in_array( get_current_user_id(), (array) $associated_user_ids );
return sprintf(
'<form method="POST" action=".">
<input type="hidden" name="edicin_id" value="%d" />
<input type="hidden" name="action" value="%s" />
%s
<button type="submit">%s</button>
</form>',
get_the_ID(),
( $is_in_user_collection ) ? 'remove' : 'add',
wp_nonce_field( 'edicin-save', '_wpnonce', true, false ),
( $is_in_user_collection ) ? __( 'Eliminar de mi colección', 'edicin' ) : __( 'Vincular a mi colección', 'edicin' ),
);
}
);
/**
* Procesar el formulario de guardado del usuario para la "edicin".
*
* Esto supone que el campo 'usuario' es una relación "multi-select".
*/
add_action(
'template_redirect',
function() {
if (
'edicin' !== get_post_type()
|| ! isset( $_POST['edicin_id'] )
|| ! isset( $_POST['action'] )
|| ! wp_verify_nonce( @$_POST['_wpnonce'], 'edicin-save' )
|| ! is_user_logged_in()
) {
return;
}
$edicin_pod = pods( 'edicin', absint( $_POST['edicin_id'] ) );
$associated_user_ids = (array) $edicin_pod->field('usuario', false );
/**
* This is necessary if "Pods Admin > Settings > Performance > Watch WP Metadata Calls" is set to:
* "Enable watching WP Metadata calls (may reduce performance with large processes)"
*/
if ( isset( $associated_user_ids[0] ) && is_array( $associated_user_ids[0] ) ) {
$associated_user_ids = wp_list_pluck( $associated_user_ids, 'ID' );
}
if ( 'add' === $_POST['action'] ) {
$edicin_pod->save([
'usuario' => array_merge(
$associated_user_ids,
[ get_current_user_id() ],
)
]);
}else if ( 'remove' === $_POST['action'] ) {
$edicin_pod->save([
'usuario' => array_diff(
$associated_user_ids,
[ get_current_user_id() ],
)
]);
}
}
);