Support

Account

Home Forums ACF PRO ACF front end insert in repeater

Solving

ACF front end insert in repeater

  • Hello, I try to create a playlist with ACF (great plugin) for a movie website.
    When the user is on single-filme.php he can add the post on his playlist.

    This is the form

    <form method="post">
         <ul>
              <li>
                   <button name="submit-playlist" onclick="<?php add_to_playlist(); ?>" value="submit-playlist"><img src="<?php echo get_template_directory_uri(); ?>/images/playlist-icon.png" alt="">Playlist</button>
              </li>
    
         </ul>
    							</form>

    and this is my function

    function add_to_playlist() {
    	if ( isset( $_POST['submit-playlist'] ) ) {
    
    		$queried_object = get_queried_object();
    		$id = $queried_object->ID;
    		$name = $queried_object->post_title;
    		$poster = get_the_post_thumbnail($id);
    		$permalink = get_the_permalink($id);
    
    		$field_key = "field_558aaa2515415";
    		$user_ID = get_current_user_id();
    		$value[] = array("field_558ab367b4756" => "Playlistul meu", "field_558aaa626db89" => array("field_558ab1d98bcfe" => $permalink));
    		update_field( $field_key, $value, 'user_'.$user_ID );
    	}
    }

    The problem is that when I press the button, it only adds the first letter of the link wich is “h” (from http). Why? I`m missing something?
    Thanks in advance.

  • Hi @sebastianbotez

    You can’t run a php function in an onclick parameter on a button. That’s for javascript only. So you’ll either have to trigger a javascript function with the onclick parameter which in term does some AJAX in order for you to do the necessary php OR you’ll have to submit the form and let the function run if a POST parameter is set:

    
    
    <?php
    	
    if ( isset( $_POST['submit-playlist'] ) ) {
    
    	//Do your submit code in here
    	
    	$queried_object = get_queried_object();
    	$id = $queried_object->ID;
    	$name = $queried_object->post_title;
    	$poster = get_the_post_thumbnail($id);
    	$permalink = get_the_permalink($id);
    
    	$field_key = "field_558aaa2515415";
    	$user_ID = get_current_user_id();
    	$value[] = array("field_558ab367b4756" => "Playlistul meu", "field_558aaa626db89" => array("field_558ab1d98bcfe" => $permalink));
    	update_field( $field_key, $value, 'user_'.$user_ID );
    }
    
    ?>
    
    <form method="post">
    	<?php if ( isset( $_POST['submit-playlist'] ) ): ?>
    		<p>Thank you, your playlist has been submitted.</p>
    	<?php endif; ?>
    	<input type="hidden" value="<?php sanitize_text_field('submit-playlist'); ?>" name="submit-playlist" />
         <ul>
              <li>
                   <button type="submit" ><img src="<?php echo get_template_directory_uri(); ?>/images/playlist-icon.png" alt="">Playlist</button>
              </li>
         </ul>
    </form>
    
    
  • Oh goddy, thank you for the solution, unfortnatelly I ca’t try it now, gonna test it tomorrow morning at work. Thank you, I’ll come back with updates!

  • Hello again, managed to add the new code. Still the same problem, I don`t get it,
    My movie name its Poltergeist (2015) but instead in field I find only letter “P” on submmit. If I check the $name var it`s full name insite. But in the field it gets echoes only first letter …. Any ideea why? Cheers buddy!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘ACF front end insert in repeater’ is closed to new replies.