Use add_action within template

There is no limitation to using add_action(), except that core must have it loaded already. Practically this means pretty much anywhere, but wp-config.php. While it is possible to do it in template, it’s not a common practice because of: Timing issues (your template part might be loaded after the hooks you need). Code base clarity … Read more

wp_ajax action is not run when ajax trigger

I’ve tested your code and it works perfectly. I’ve just declared the dependencies for the JavaScript, so it is loaded correctly after jQuery. This is how I’ve tested it: functions.php function my_resource() { wp_enqueue_script(‘my-jquery’,get_template_directory_uri().’/jqfunctions.js’, array(‘jquery’)); wp_localize_script( ‘my-jquery’, ‘myback’, array(‘ajax_url’ => admin_url( ‘admin-ajax.php’ ))); } add_action(‘wp_enqueue_scripts’, ‘my_resource’); function getsomething(){ wp_send_json_error(‘hey’); } add_action(‘wp_ajax_nopriv_getsomething’, ‘getsomething’); add_action(‘wp_ajax_getsomething’, ‘getsomething’); jqfunctions.js … Read more

Dynamically adding filters

I think you are on the right track, but that last part is messy. You are using the bw_add_markup_class function for two different purposes : to return the classes that you want to add to list the context of the filters that you want to call If I understand well want you are trying to … Read more