Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

99
Vistas
Javascript Ajax llama a functions.php usando wordpress

Hola, estoy siguiendo un tutorial sobre llamadas Ajax en wordpress. El ejemplo tiene todo el código en functions.php pero quiero ser más organizado e incluir el javascript en un archivo js.

Esto es lo que tengo hasta ahora

funciones.php

 //The PHP function example_ajax_request() { // The $_REQUEST contains all the data sent via AJAX from the Javascript call if ( isset($_REQUEST) ) { $fruit = $_REQUEST['fruit']; // This bit is going to process our fruit variable into an Apple if ( $fruit == 'Banana' ) { $fruit = 'Apple'; } // Now let's return the result to the Javascript function (The Callback) echo $fruit; } // Always die in functions echoing AJAX content die(); } // This bit is a special action hook that works with the WordPress AJAX functionality. add_action( 'wp_ajax_example_ajax_request', 'example_ajax_request' ); add_action( 'wp_ajax_nopriv_example_ajax_request', 'example_ajax_request' );

y en el archivo Javascript test.js:

 var ajaxurl = "' . admin_url('admin-ajax.php') . '"; // This is the variable we are passing via AJAX var fruit = 'Banana'; // This does the ajax request (The Call). $( ".banana" ).click(function() { $.ajax({ url: ajaxurl, // Since WP 2.8 ajaxurl is always defined and points to admin-ajax.php data: { 'action':'example_ajax_request', // This is our PHP function below 'fruit' : fruit // This is the variable we are sending via AJAX }, success:function(data) { // This outputs the result of the ajax request (The Callback) $(".banana").text(data); window.alert("here"); }, error: function(errorThrown){ window.alert(errorThrown); } }); });

Cómo puede: 'action':'example_ajax_request', // This is our PHP function below

se puede acceder desde el archivo functions.php?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda