url_to_postid returns 0

when you are at the post URL, you don’t need to work with referer (which is not reliable moreover) but with $GLOBALS[“post”] because you are in the PHP code which generate the page then you can have the post ID with that : $post_id = $GLOBALS[“post”]->ID;

Why does my jQuery plugin show up as text in WordPress?

You can use this PHP code to output your slider’s initiator in your themes footer: add_action(‘wp_footer’,’initiate_unslider’); function initiate_unslider () { ?> <script src=”https://code.jquery.com/jquery-2.1.4.min.js”></script> <script src=”/path/to/unslider.js”></script> <script> $(document).ready(function() { $(‘.banner’).unslider(); }); </script> <?php } Also it’s safer to add a $(document).ready before initiating your slider, in case some necessary components of your page has not been … Read more

How to reference PHP in Javascript

To answer the question, no: It’s not an issue with formatting, it’s as jdm2112 explains the matter. You can, however, “send” the variable , or have it plucked from the server, either via an Ajax call as jdm2112 suggests or as set by wp_localize_script(). Which you use will likely depend on what the script does … Read more

Twenty Fifteen: Change navigation menu behavior

In case anyone is interested, I think I managed to find a solution. Just had to add the following two lines of code just after the e.preventDefault(); line and before the _this.toggleClass( ‘toggle-on’ ); one: container.find( ‘.dropdown-toggle.toggle-on’ ).not( _this ).not( _this.parents( ‘.children, .sub-menu’ ).prev( ‘.dropdown-toggle’ ) ).removeClass( ‘toggle-on’ ).attr( ‘aria-expanded’, false ); container.find( ‘.children.toggled-on, .sub-menu.toggled-on’ … Read more

Headless wp with react. How to handle routes?

You will need to add react-router-dom with npm or yarn. Once you add those, you will need to import some modules into your React app. You will need BrowserRouter, Route, NavLink, & withRouter. BrowserRouter is to be on the outmost parent Component that manages the history API. Ideally, you would just put your <App /> … Read more

How To Render Shortcode In AJAX Response?

In general, sshortcodes are not a programming construct, they are content macros. As content macros, their viability in being used outside of their content context is limited. Simplistic shortcodes, like the gallery/audio/video (and I am not sure even about them) might “render” without context, but advanced ones will need a properly set loop, and calls … Read more