wp_localized_script is not defined when called via jquey ajax

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I am struggling to get my ajax request to work. It fails when I fire my $ajax and I get this error...



Uncaught ReferenceError: feature_ajax is not defined



This is my functions.php



// load our front-end modifiers
require_once(__DIR__ . '/lib/Frontend.lib.php');


This is my frontend class php...



global $wp_query;

class Frontend


/** frontend constructor */
public function __construct()


// enqueue our scripts and shit
add_action('wp_enqueue_scripts', array($this, 'action_wp_enqueue_scripts'));

// add out ajax actions
$this->ajax_actions();



/** frontend enqueued scripts */
public function action_wp_enqueue_scripts()


// localize admin ajax
wp_localize_script('feature-ajax', 'feature_ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));

// enqueue scripts
wp_enqueue_script('feature-ajax');




/** ajax actions */
public function ajax_actions()


// feature data ajax actions
add_action('wp_ajax_feature_data', 'my_feature_data');
add_action('wp_ajax_nopriv_feature_data', 'my_feature_data');




/** my feature post data */
public function my_feature_data()

global $post;
$post_id = ($_REQUEST['id']);

if($post_id)
$post = get_post($post_id);
setup_postdata($post);
get_template_part('content','ajax');
die();






new Frontend();


When I fire the $ajax script below, this is when I get the error feature_ajax is not defined.



But it is defined in the code above.



// load feature post data
$.ajax(
cache: false,
timeout: 8000,
url: feature_ajax.ajaxurl,
type: 'POST',
data:
action: 'feature_data',
id: feature_id
,
success: function (data)
alert(data);

);


Any help in understanding what i'm doing wrong would be great. I followed two tutorials and still cant get to the success callback :(



Thanks










share|improve this question









New contributor




joshmoto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Have you considered using the REST API instead? It's much easier to debug, and more straightforward to register
    – Tom J Nowell♦
    1 hour ago










  • I'll take a look at it thanks
    – joshmoto
    1 hour ago
















up vote
1
down vote

favorite












I am struggling to get my ajax request to work. It fails when I fire my $ajax and I get this error...



Uncaught ReferenceError: feature_ajax is not defined



This is my functions.php



// load our front-end modifiers
require_once(__DIR__ . '/lib/Frontend.lib.php');


This is my frontend class php...



global $wp_query;

class Frontend


/** frontend constructor */
public function __construct()


// enqueue our scripts and shit
add_action('wp_enqueue_scripts', array($this, 'action_wp_enqueue_scripts'));

// add out ajax actions
$this->ajax_actions();



/** frontend enqueued scripts */
public function action_wp_enqueue_scripts()


// localize admin ajax
wp_localize_script('feature-ajax', 'feature_ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));

// enqueue scripts
wp_enqueue_script('feature-ajax');




/** ajax actions */
public function ajax_actions()


// feature data ajax actions
add_action('wp_ajax_feature_data', 'my_feature_data');
add_action('wp_ajax_nopriv_feature_data', 'my_feature_data');




/** my feature post data */
public function my_feature_data()

global $post;
$post_id = ($_REQUEST['id']);

if($post_id)
$post = get_post($post_id);
setup_postdata($post);
get_template_part('content','ajax');
die();






new Frontend();


When I fire the $ajax script below, this is when I get the error feature_ajax is not defined.



But it is defined in the code above.



// load feature post data
$.ajax(
cache: false,
timeout: 8000,
url: feature_ajax.ajaxurl,
type: 'POST',
data:
action: 'feature_data',
id: feature_id
,
success: function (data)
alert(data);

);


Any help in understanding what i'm doing wrong would be great. I followed two tutorials and still cant get to the success callback :(



Thanks










share|improve this question









New contributor




joshmoto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Have you considered using the REST API instead? It's much easier to debug, and more straightforward to register
    – Tom J Nowell♦
    1 hour ago










  • I'll take a look at it thanks
    – joshmoto
    1 hour ago












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am struggling to get my ajax request to work. It fails when I fire my $ajax and I get this error...



Uncaught ReferenceError: feature_ajax is not defined



This is my functions.php



// load our front-end modifiers
require_once(__DIR__ . '/lib/Frontend.lib.php');


This is my frontend class php...



global $wp_query;

class Frontend


/** frontend constructor */
public function __construct()


// enqueue our scripts and shit
add_action('wp_enqueue_scripts', array($this, 'action_wp_enqueue_scripts'));

// add out ajax actions
$this->ajax_actions();



/** frontend enqueued scripts */
public function action_wp_enqueue_scripts()


// localize admin ajax
wp_localize_script('feature-ajax', 'feature_ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));

// enqueue scripts
wp_enqueue_script('feature-ajax');




/** ajax actions */
public function ajax_actions()


// feature data ajax actions
add_action('wp_ajax_feature_data', 'my_feature_data');
add_action('wp_ajax_nopriv_feature_data', 'my_feature_data');




/** my feature post data */
public function my_feature_data()

global $post;
$post_id = ($_REQUEST['id']);

if($post_id)
$post = get_post($post_id);
setup_postdata($post);
get_template_part('content','ajax');
die();






new Frontend();


When I fire the $ajax script below, this is when I get the error feature_ajax is not defined.



But it is defined in the code above.



// load feature post data
$.ajax(
cache: false,
timeout: 8000,
url: feature_ajax.ajaxurl,
type: 'POST',
data:
action: 'feature_data',
id: feature_id
,
success: function (data)
alert(data);

);


Any help in understanding what i'm doing wrong would be great. I followed two tutorials and still cant get to the success callback :(



Thanks










share|improve this question









New contributor




joshmoto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am struggling to get my ajax request to work. It fails when I fire my $ajax and I get this error...



Uncaught ReferenceError: feature_ajax is not defined



This is my functions.php



// load our front-end modifiers
require_once(__DIR__ . '/lib/Frontend.lib.php');


This is my frontend class php...



global $wp_query;

class Frontend


/** frontend constructor */
public function __construct()


// enqueue our scripts and shit
add_action('wp_enqueue_scripts', array($this, 'action_wp_enqueue_scripts'));

// add out ajax actions
$this->ajax_actions();



/** frontend enqueued scripts */
public function action_wp_enqueue_scripts()


// localize admin ajax
wp_localize_script('feature-ajax', 'feature_ajax', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));

// enqueue scripts
wp_enqueue_script('feature-ajax');




/** ajax actions */
public function ajax_actions()


// feature data ajax actions
add_action('wp_ajax_feature_data', 'my_feature_data');
add_action('wp_ajax_nopriv_feature_data', 'my_feature_data');




/** my feature post data */
public function my_feature_data()

global $post;
$post_id = ($_REQUEST['id']);

if($post_id)
$post = get_post($post_id);
setup_postdata($post);
get_template_part('content','ajax');
die();






new Frontend();


When I fire the $ajax script below, this is when I get the error feature_ajax is not defined.



But it is defined in the code above.



// load feature post data
$.ajax(
cache: false,
timeout: 8000,
url: feature_ajax.ajaxurl,
type: 'POST',
data:
action: 'feature_data',
id: feature_id
,
success: function (data)
alert(data);

);


Any help in understanding what i'm doing wrong would be great. I followed two tutorials and still cant get to the success callback :(



Thanks







wp-localize-script






share|improve this question









New contributor




joshmoto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




joshmoto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago





















New contributor




joshmoto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









joshmoto

1084




1084




New contributor




joshmoto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





joshmoto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






joshmoto is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Have you considered using the REST API instead? It's much easier to debug, and more straightforward to register
    – Tom J Nowell♦
    1 hour ago










  • I'll take a look at it thanks
    – joshmoto
    1 hour ago
















  • Have you considered using the REST API instead? It's much easier to debug, and more straightforward to register
    – Tom J Nowell♦
    1 hour ago










  • I'll take a look at it thanks
    – joshmoto
    1 hour ago















Have you considered using the REST API instead? It's much easier to debug, and more straightforward to register
– Tom J Nowell♦
1 hour ago




Have you considered using the REST API instead? It's much easier to debug, and more straightforward to register
– Tom J Nowell♦
1 hour ago












I'll take a look at it thanks
– joshmoto
1 hour ago




I'll take a look at it thanks
– joshmoto
1 hour ago










1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










To successfully add variable to the window object via wp_localize_script you need to properly invoke three functions in the following sequence:



wp_register_script( string $handle, string $src, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false );
wp_localize_script( string $handle, string $object_name, array $l10n )
wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )


In your case you're missing the wp_register_script. I hope this helps.






share|improve this answer




















  • Ahhh i see i was already wp_register_script('theme-js', ...) so i've just done this now wp_localize_script('theme-js', ... ) and it works now thank you dude
    – joshmoto
    1 hour ago











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "110"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






joshmoto is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f316199%2fwp-localized-script-is-not-defined-when-called-via-jquey-ajax%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










To successfully add variable to the window object via wp_localize_script you need to properly invoke three functions in the following sequence:



wp_register_script( string $handle, string $src, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false );
wp_localize_script( string $handle, string $object_name, array $l10n )
wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )


In your case you're missing the wp_register_script. I hope this helps.






share|improve this answer




















  • Ahhh i see i was already wp_register_script('theme-js', ...) so i've just done this now wp_localize_script('theme-js', ... ) and it works now thank you dude
    – joshmoto
    1 hour ago















up vote
2
down vote



accepted










To successfully add variable to the window object via wp_localize_script you need to properly invoke three functions in the following sequence:



wp_register_script( string $handle, string $src, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false );
wp_localize_script( string $handle, string $object_name, array $l10n )
wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )


In your case you're missing the wp_register_script. I hope this helps.






share|improve this answer




















  • Ahhh i see i was already wp_register_script('theme-js', ...) so i've just done this now wp_localize_script('theme-js', ... ) and it works now thank you dude
    – joshmoto
    1 hour ago













up vote
2
down vote



accepted







up vote
2
down vote



accepted






To successfully add variable to the window object via wp_localize_script you need to properly invoke three functions in the following sequence:



wp_register_script( string $handle, string $src, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false );
wp_localize_script( string $handle, string $object_name, array $l10n )
wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )


In your case you're missing the wp_register_script. I hope this helps.






share|improve this answer












To successfully add variable to the window object via wp_localize_script you need to properly invoke three functions in the following sequence:



wp_register_script( string $handle, string $src, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false );
wp_localize_script( string $handle, string $object_name, array $l10n )
wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )


In your case you're missing the wp_register_script. I hope this helps.







share|improve this answer












share|improve this answer



share|improve this answer










answered 1 hour ago









Den Isahac

441210




441210











  • Ahhh i see i was already wp_register_script('theme-js', ...) so i've just done this now wp_localize_script('theme-js', ... ) and it works now thank you dude
    – joshmoto
    1 hour ago

















  • Ahhh i see i was already wp_register_script('theme-js', ...) so i've just done this now wp_localize_script('theme-js', ... ) and it works now thank you dude
    – joshmoto
    1 hour ago
















Ahhh i see i was already wp_register_script('theme-js', ...) so i've just done this now wp_localize_script('theme-js', ... ) and it works now thank you dude
– joshmoto
1 hour ago





Ahhh i see i was already wp_register_script('theme-js', ...) so i've just done this now wp_localize_script('theme-js', ... ) and it works now thank you dude
– joshmoto
1 hour ago











joshmoto is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















joshmoto is a new contributor. Be nice, and check out our Code of Conduct.












joshmoto is a new contributor. Be nice, and check out our Code of Conduct.











joshmoto is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f316199%2fwp-localized-script-is-not-defined-when-called-via-jquey-ajax%23new-answer', 'question_page');

);

Post as a guest













































































Comments

Popular posts from this blog

Long meetings (6-7 hours a day): Being “babysat” by supervisor

Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

Confectionery