Search code examples
phpwordpresswordpress-plugin-creation

Adding .php extension to one plugin page in Wordpress


I'm searching for a solution to add .php extension to the page of my wordpress plugin.

I already found similar posts but none of them was about adding the .php extension to only on page generated by a plugin.

I already tried to work with the global $wp_rewrite; but it would apply the .php extension to all pages.

All I want is simply something like that:

www.mydomain.com/myfile => www.mydomain.com/myfile.php but only for this one particular page.

Update: The file myfile.php doesn't exist. It's the permalink of a wp page. I basically want to add .php to the permalink of one page (only this one page). I know it's possible to change the permalink of an page, but wp would not let me add .php to the permalink. It automatically changes it to -php (it doesn't accept the dot).


Solution

  • It is not necessary to change the link format for all pages via wp_rewrite. You can add rewrite_rule for a specific page without changing the format for the entire Post Type.

        add_rewrite_rule(
          '^myfile.php',
          'index.php?page_id=1234',
          'top'
      ); 
    

    1234 = your page $post_id

    Next, we change permalink generation for our page

    add_filter( 'page_link', 'filter_function_myfile_link', 10, 3 );
    function filter_function_myfile_link( $link, $post_id, $sample ){
    
    if ( $post_id == 1234  ) {
    $link = home_url('/myfile.php', 'https');
    }
    
        return $link;
    }