0

I am loading new form fields from Ajax when I click on radio button in my custom widget. In Ajax callback handler, I need to use get_field_name and get_field_id widget class methods. get_field_name method is return in WP_Widget class. So I try to get the instant of that class. But I unable to do that. So please someone help me how to use it in external class.

   function wp_ajax_suport(){

    add_action('wp_ajax_my_func', 'my_func');
    add_action('wp_ajax_nopriv_my_func', 'my_func');
    }    
    add_action( 'init', 'wp_ajax_suport' );

    function my_func(){

    <input type="text" name="<?php $this->get_field_name('name'); ?>">
    }
  • just register your ajax actions properly - using external files causes more issues than it would serve any use. – Martin Zeitler Mar 24 '14 at 6:35
  • @syslogic: i am using registering ajax in proper way. I unable to use the function get_field_name inside ajax handler. Check my code sample – Gowri Mar 24 '14 at 6:38
  • you have to register both, frontend and backend hooks, when using wp-ajax on the frontent. seems unlogical - but that's the way it works. – Martin Zeitler Mar 25 '14 at 8:18
  • Syslog it not related to my question – Gowri Mar 25 '14 at 8:43
  • @syslogic: what you mean by proper way!. I understood what you saying front/back end ajax register. I am already doing it. But my actual question is entirely different from here. I want to access $this->get_field_name function in ajax handler. – Gowri Mar 28 '14 at 10:51
0

Why you want to use ajax for showing some fields on form in widget? Can't it be done using simple show hide using jquery. See below. Not tested , just to give insight.

HTML

<input type="radio" id="someradio" />
<div id="somediv" style="display:none;">
    <input type="text" name="<?php $this->get_field_name('name'); ?>">
</div>

Jquery

<script type="application/javascript">
    jQuery('#someradio').on('click',function(){
        jQuery('#somediv').toggle();
    });
</script>
  • Thanks for helping. But I loading form fields throw ajax. because it calls api. I don't want to waste the api calles for every load of the page. So I am calling ajax and loading the new dynamic form fields depends on selecting the checkbox. So I want to know how to use the get_field_name out side or is there a way to store the widget without that name function. – Gowri Mar 24 '14 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.