Tuesday, August 23, 2022
HomeWordPress Developmentwpdb - Wordpress ajax response save into database

wpdb – WordPress ajax response save into database


I’ve this code which seize playlist from youtube and thru foreach and ajax ship information to wordpress operate to avoid wasting into database. IT just isn’t working, i do know someplace i am doing unsuitable however unable to search out. followin is my code

add_action('admin_footer','add_into_playlist_function');
operate add_into_playlist_function() {
    ?>
    <script src="https://apis.google.com/js/api.js"></script>
    <script sort="textual content/javascript">
        jQuery(doc).prepared(operate($){            
            $('.add-playlist').on('click on', operate(e){       
                e.preventDefault();                
                var url="https://www.googleapis.com/youtube/v3/playlists?half=snippet,contentDetails&key=API_KEY&channelId=CHANNEL_ID&maxResults=50";
                $.getJSON(url).performed(operate(response){
                    //console.log(response);
                    response["items"].forEach( operate( worth, cxv){
                        //console.log(worth.id);
                        var information = {
                            'motion': 'save_playlist_data',
                            'safety': '<?php echo wp_create_nonce( "wpdocs-special-string" ); ?>',
                           'playlist_code': worth.id,
                           'playlist_title': worth.snippet.title,
                           'playlist_image': worth.snippet.thumbnails.medium.url,
                        }
                        $.ajax({
                            url: '<?php echo admin_url( "admin-ajax.php" ); ?>',
                            information: information,
                            methodology: 'POST',
                            dataType: 'json',
                            success ( res ) {
                                if ( ! res.success ) {
                                    console.log( res.information );
                                } else {
                                    console.log( res );
                                }
                            },
                            error ( xhr, error, standing ) {
                                console.log( error, standing );
                            }
                        });
                    });
                });                
            });
        });
    </script>
    <?php
}

operate save_data_table() {
    world $wpdb;
    $output = ['status' => 1];
    if ( ! check_ajax_referer( 'wpdocs-special-string', 'safety', false ) ) {
        wp_send_json_error( 'Invalid Request' );
    }
    
    $playlist_code = $_POST['playlist_code'];
    $playlist_title = $_POST['playlist_title'];
   $playlist_image = $_POST['playlist_image'];
    $information = array(
        'playlist_code' => $playlist_code,
        'playlist_image' => $playlist_image,
        'playlist_title' => $playlist_title,
    );
    $wpdb->insert( 'wp_youtube_playlist', $information );
    $output['status'] = 2;
    wp_send_json($output);
    
}
add_action( 'wp_ajax_save_playlist_data', 'save_data_table' );
add_action( 'wp_ajax_nopriv_save_playlist_data', 'save_data_table');

kindly appropriate me the place i am unsuitable. thanks thousands and thousands

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments