I am creating a plugin that allows users to insert certain code on their specified page or post of their choice. I have tried doing this:

function asdf() {

    $siteURL = get_site_url() . '/';
    $pages = get_pages();

     foreach ($pages as $page ) {

         $pageTitle = get_page_link( $page->ID );

       if ( $siteURL === $pageTitle ) {
          echo "this is the correct page" . "<br>";

       else {
          echo "this is the wrong page". "<br>";

add_action('wp_head', 'asdf');

If you run this code, you will see that it treats all pages the same, and there would be no distinction in the results across different pages. However I am trying to get my function to treat each page uniquely, and give different results based on each page title.

Thanks in advance.

1 Answer 1


to test if you are on specific page, you just need to test the identifier like that :

add_action("wp_head", function () {

    $idPageToTest = 32867;

    if (    isset($GLOBALS["post"])
        &&  ($GLOBALS["post"]->ID === $idPageToTest)
    ) {
        echo "We are on page $idPageToTest.";


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.