Skip to content

Instantly share code, notes, and snippets.

@Njengah
Last active March 2, 2020 19:00
Show Gist options
  • Save Njengah/bb52b6571fb7e0bbb105bfb6d4072422 to your computer and use it in GitHub Desktop.
Save Njengah/bb52b6571fb7e0bbb105bfb6d4072422 to your computer and use it in GitHub Desktop.
WordPress function to get post id by slug and test it with an action hook to wp_head for tutorial - https://njengah.com/get-post-id-by-slug-in-wordpress/
<?php
/**
* Function to get post ID By slug
*/
function get_id_by_slug($page_slug, $slug_page_type = 'page') {
$find_page = get_page_by_path($page_slug, OBJECT, $slug_page_type);
if ($find_page) {
return $find_page->ID;
} else {
return null;
}
}
/**
* Testing by Display to Header
*/
add_action('wp_head' , 'test_function');
function test_function(){
echo get_id_by_slug('login-page','page');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment