To get a permalink you need to use get_permalink()
. Just pass the ID of the post or leave it blank to get the current page in the Loop.
wp_localize_script ()
will render PHP variables on the page that are consumable by JS through a global object that you define.
PHP
// Register the script
wp_register_script( 'some_handle', 'path/to/myscript.js' );
// Localize the script with new data
$translation_array = array(
'some_string' => __( 'Some string to translate', 'plugin-domain' ),
'a_value' => '10'
);
wp_localize_script( 'some_handle', 'object_name', $translation_array );
// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );
JS
<script>
// alerts 'Some string to translate'
alert( object_name.some_string);
</script>
The REST-API is also always available for you to extend how you see fit: http://v2.wp-api.org/extending/adding/
Related Posts:
- Automatically append ID to at the end of specific URLs
- Menu Link Redirect Based on Username or User ID
- How to get this JavaScript working with get_permalink
- WordPress 3.1 not compatible with jQuery Mobile?
- Change in permalink structure made javascript cookie not work correctly
- Gutenberg Editor: dynamically edit slug field based on ACF field
- Edit Post Permalink option removed, looking for possible causes
- Why not working /%postname%/ with ajax serverinterval?
- Include other parameters in permalinks for better management of browser history
- Can you customize the automatic permalink population on new posts?
- How do I disable the changing of the permalink for published posts (for non-admin)?
- How can I include permalinks in this code?
- Remove some rewrite rules in wordpress
- When do i need to get permalink structure of a specific post?
- Changing WordPress routing to load the same page for different URLs
- Add a filter to get_comments_link()
- Custom post type clean shortlink
- “JQMIGRATE: Migrate is Installed” How do I find the problem code? [duplicate]
- Is it save to use eval for a jQuery callback method coming from the database?
- Referrer URL with jQuery or Javascript – Cache & Referrer URL Issue
- Jquery window.send_to_editor function
- Why doesn’t jQuery use $, and how can I get it do so (GLOBALLY)?
- cannot link a external url, present page url acting as parent url
- Permalink for specific page name
- How Do I get the author’s page id?
- the_permalink on the homepage vs posts for sharing links
- WordPress Installed But Not Showing Up! [closed]
- wordpress add_action() issue in ajax call
- Echo title with permalink
- JQuery Plugins in WordPress
- Permalink for PDF of article
- Create special WordPress slideshow with “jquery.cycle.all.js”
- Using Foundation 5 in WordPress Administration Area
- Media/attachment urls don’t redirect with fake URL structure
- Can WP give a value a post ID?
- I have an old version of jQuery showing up. How do I figure out where it is coming from?
- Remove parent page from url
- Some Pemalink problems, probably caused by wrong .htaccess configuration?
- Plugin Scripts no loading on options page
- Close Fancybox, sumbit form data and redirect
- How to link the title adding a permalink?
- How do I include javascript/jquery image resizing with wordpress?
- Visiting posts results in 404 errors after host migration
- Suddenly new posts have the default permalink instead of the post name
- Localizing methods for jQuery append prepend wrap parent
- Permalink Settings
- Using one slug/post-name for multiple pages
- Ajax Validation for reCaptcha
- implement separate templates for 1 post type
- How can I redirect this contact form to a specific permalink
- get_posts post title permalink not working for last item in array
- Retrieve default permalink while using seo friendly URLs in WordPress
- Change permalink structure for pagination only
- encoded search terms with %20
- How do I make my wordpress post appear in a mouse hover preview?
- How to enque js script in footer that are not in .js file?
- Using JQuery to check for Rel Attribute of Image before Overriding It
- Permalinks not working on new site
- Problems with rewrite rule
- How to Custom Edit Post Title & Permalink Slug?
- Test site pages go to main site
- Importer failed to import posts as format “day and name”
- Problem with parent page slug only in WordPress admin
- Comment author url link is wrong? How to change from authors/ to author/
- when I create a page with a /blog permalink the css gets messed up
- Loading scripts on a single page plants the script in the footer
- Getting a ‘slide down’ js panel implemented within WP
- How to redirect RSS feeds to Feedburner and keep pretty permalinks?
- Properly embed javascript into WP (using function.php) – doesn’t work?
- Page Name and Permalinks
- Search input area autogrow – how?
- Using jquery and javascript in WordPress
- Remove “attachment/” from the URL of attachment pages
- Multiple permalinks
- rename ‘page’ URL fragment in pretty permalinks
- Nested Gallery Shortcode INTERMITTENTLY working
- Permalinks set to /%postname%/ but still have links being indexed in the form ?p=x
- Can’t get permalinks working!
- How to add drag and drop functionalities to a div inside option panel
- How do I change the permalink structure without getting 404s?
- Run a jquery script on on a certain template page
- Strange behavior with a redirect loop – involves WPML multi-language plugin
- RSS feed link on archives page not working
- Custom URL rewrites for templates
- WordPress author.php template does not work with custom permalink structure! Help!
- Gutenberg output URL to post from attribute with post ID
- On click load iframe [closed]
- Uncaught ReferenceError: jQuery is not defined – although jQuery is loaded [closed]
- Broken wordpress site, non-working links and no access to wp-admin page
- How to change permalinks
- Uncaught TypeError: jQuery(…).slider is not a function
- Adding a prefix to the blog, but only the blog
- Inserting a string in the middle of page’s permalink?
- Strange behavior: random HTTP error 500 fixed by visiting Permalinks settings page (htaccess issue with language code)
- Why can’t change the permalink of my page/post?
- Getting back a permalink from custom url
- how to get a list of all top level pages
- Permalinks on Windows IIS not working and web.config breaks site – does wordpress not support windows server now, if so how?
- Need help about understand api, wp, $ syntax in WordPress plugin script
- Disable Cloudflare Rocket Loader for jQuery javascript and make it load first