Home › Forum › Problemi Vari con WP › [ajax] richiamare da pagina frontend una pagina backend
-
AutorePost
-
-
16 Gennaio 2016 alle 15:12 #30505follettinuxPartecipante
Da una pagina frontend devo richiamare una pagina del backend ma funziona soltanto se sono logato come amministratore ovviamente.
Lo script nella pagina è:
<script>
jQuery(document).ready( function($) {
$.ajax({
url: "/wp-admin/edit.php?post_type=download&page=.....",
success: function( data ) {
// messaggio ok
}
})
})
</script>
Come faccio a farlo partire anche da utente semplice o anonimo?
-
16 Gennaio 2016 alle 15:16 #119578wollyAmministratore del forum
-
16 Gennaio 2016 alle 16:27 #119580follettinuxPartecipante
ho provato a seguire quanto scritto e ho provato con un esmpio ma non mi funzione.
Nella pagina del frontend ho scritto
<?php
add_action( 'admin_footer', 'my_action_javascript' ); // Write our JS below here
function my_action_javascript() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
'action': 'my_action',
'whatever': 1377
};
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
</script> <?php
}
?>
Invece nella pagina function.php del mio tema ho messo:
add_action( 'wp_ajax_my_action', 'my_action_callback' );
function my_action_callback() {
global $wpdb; // this is how you get access to the database
$whatever = intval( $_POST );
$whatever += 10;
echo $whatever;
wp_die(); // this is required to terminate immediately and return a proper response
}
in questo modo nella pagina frontend dovrebbe apparire il risultato di 1377 + 10 ma non succede nulla -
16 Gennaio 2016 alle 18:16 #119583wollyAmministratore del forum
Quelle sono le istruzioni, seguendole mi è sempre fuzionato, non sparei come altro aiutarti.
-
16 Gennaio 2016 alle 18:17 #119584wollyAmministratore del forum
Ajax on the Viewer-Facing Side
Since WordPress 2.8, there is a hook similar to wp_ajax_(action):
wp_ajax_nopriv_(action) executes for users that are not logged in.
So, if you want it to fire on the front-end for both visitors and logged-in users, you can do this:
add_action( ‘wp_ajax_my_action’, ‘my_action_callback’ );
add_action( ‘wp_ajax_nopriv_my_action’, ‘my_action_callback’ );
-
-
AutorePost
- Devi essere connesso per rispondere a questo topic.