Skip to content

Instantly share code, notes, and snippets.

@chokonet
Last active December 26, 2015 19:59
Show Gist options
  • Save chokonet/7205620 to your computer and use it in GitHub Desktop.
Save chokonet/7205620 to your computer and use it in GitHub Desktop.
enviar variables por ajax a php
function ajax_favoritos(post_id, user_id) {
return $.post(ajax_url, {
post_id: post_id,
user_id: user_id,
action: 'marcar_favoritos'
}, 'json');
}
<?php
//localisar la variable para enviar datos por ajax
wp_localize_script('functions', 'ajax_url', admin_url('admin-ajax.php'));
<?php
/**
* Crea campos en la tabla wp_post_favorito.
*
* @param $_POST['post_id']
* @param $_POST['id_user']
*/
function marcar_favoritos(){
$post_id = isset($_POST['post_id']) ? $_POST['post_id'] : false;
$favorito = isset($_POST['favorito']) ? $_POST['favorito'] : false;
$facebook_user = get_facebook_user();
if ( ! $facebook_user AND ! $post_id ) return;
$existe = check_if_actividad_exists($facebook_user, $post_id);
if ($existe){
$result = update_actividad($facebook_user, $post_id, $favorito);
} else {
$result = create_new_actividad($facebook_user, $post_id, $favorito);
}
echo json_encode($result);
exit;
}
add_action('wp_ajax_administrar_favoritos', 'administrar_favoritos');
add_action('wp_ajax_nopriv_administrar_favoritos', 'administrar_favoritos');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment