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?
- Toggle show/hide on click with jQuery
- When and why to ‘return false’ in JavaScript?
- querySelector vs. getElementById
- How to generate a simple popup using jQuery
- Create table with jQuery – append
- When is JavaScript synchronous?
- How to reset a form using jQuery with .reset() method
- Load image with jQuery and append it to the DOM
- Change text on hover, then return to the previous text
- How can I expand and collapse a using javascript?
- What is the non-jQuery equivalent of ‘$(document).ready()’?
- Changing the selected option of an HTML Select element
- where is permalink info stored in database?
- WordPress Permalink changes to question mark (?) in URL
- Test to see if jQuery or Prototype is queued by another plugin?
- Can WordPress realistically handle a site with 500 ‘pages’?
- jQuery in header or footer
- How to prevent the default home rewrite to a static page
- Is there an action for when permalinks are rebuilt?
- WordPress Ajax always returns a 404 error
- How to get url of a post from admin panel
- How to get a page url by a page id?
- Modify Custom Field in Media Library using JavaScript
- CPT archive 404ing when using a custom taxonomy name as a variable
- How to change “permalink” structure in “WordPress Thesis” theme?
- Performance and styles not working fine after changing permalink structure
- Maintaining two permalink structures
- removing index.php IIS 7.5 webconfig
- Permalinks Messed up
- rewrite_rule for custom post type doesn’t affect get_permalink
- Add ‘articles’ prefix before blog posts url without affecting pagination
- Click loads template via ajax
- 404 Page Not Found for Only One Post
- How to 301 all posts
- WordPress within specific sub-directories, implicitly not root
- Custom attachment permalink structure [closed]
- Call to a member function add_rule() on a non-object
- Link to blog index from template
- Auto-detect javascript errors and warn theme user
- How to change Post ID during import
- combining js scripts from a js newb [closed]
- Is there anything built into the WordPress core to enable me to get the relative path from the “permalink structure” option
- Heartbeat API: How to access data already enqueued?
- Permalink change made author archive vanished
- WordPress 404 in development area
- js file in root loading without
- Custom Permalink
- Pagination Issue – /page/2 404
- Nginx Rewrite Rule:: index.html added to every permalink
- Redirect a Blogger Page URL to wordpress
- How to prevent redirect when using the term “page” in query string?
- Edit page slug after save, cause 404 by linked pages
- Fancybox type popup window that’s not an iframe
- Problem passing id-specific objects to javascript via wp_localize_script
- Custom function making troubles in get_permalink for ACF relationship fields
- .htaccess rewriterule being ignored – tyring to remove dates from WP posts
- How to redirect RSS feeds to Feedburner and keep pretty permalinks?
- Properly embed javascript into WP (using function.php) – doesn’t work?
- Why does my jQuery plugin show up as text in WordPress?
- What happens to the permalink that use %postname% if I change the post name
- Assigning proper permalink to blog that is part of a website
- Change Query Arguments (filter) with jQuery/Ajax or PHP?
- What would cause a 500 Server Error with permalinks turned OFF?
- Changing permalinks back to default ( ?p=123 )
- permalinks on title tag
- Double Slash After Media Path (uploads//)
- Tracking Visitor LatLng with WordPress using JS, PHP. How to put data which was extract using JS into DB
- New posts link to old posts (random)
- Admin-ajax.php 404 error
- How to pass values from one function to the other using an array variable
- Custom Permalink (with category) for Pages
- 301 redirects after changing permalinks
- Post links broken after migration
- How to submit form data in the same page in WordPress without reloading the page?
- WordPress All pages but home simply read “hello”
- ACF + WP + Slick Slider Repeater Slide Group PHP not Displaying [closed]
- How to change link of an uploaded file to the media library
- On Macbook, my permalinks with post name don’t work. Returning 404 error
- WordPress pages break with custom permalink settings (theme directory not output)
- Difference and usage of uri (e.g. get_directory_uri) and absolute path (e.g. get_directory)
- Fresh wordpress installation in sub-directory not accessible
- Get link from wpe_excerpt “read more” and move it
- WordPress 4.0 beta – how to change the link URL in RSS feeds
- Folder Name and Category Name WordPress
- Search points to /search/?s=KEYWORD instead of /search/KEYWORD
- How to change pagination base from slash to query form?
- WP Custom Permalink Filter
- standard post with extra parameter brings permalink problem
- Siteurl code for links
- Javascript as Jquery Function Call?