S
S
Sergey Krivosheev2018-03-16 11:43:06
WordPress
Sergey Krivosheev, 2018-03-16 11:43:06

Wordpress hook to call a function without creating a page?

Good afternoon.
Tried to create my pages in Wordpress. I didn't find the perfect solution. It turned out to intercept the url and insert the php text there. But the title, etc. still remained "page not found"
What I need:
1. at the specified path in the url (for example, site.ru/myfunction), the function myfunction () function in the index.php base template was opened for execution (without creating "pages" with such name in the admin panel)
2. make this interception in the plugin.
Now implemented through shortcodes. But this is very inconvenient from a programming point of view. Those. it’s easier to implement a ready-made piece of code than to create a page, insert a shortcode into it, etc.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
E, 2018-03-17
@aylo

i would do something like that

add_action( 'wp', 'generic_content' );
function generic_content() {
  global $wp_query;

  if ( $wp_query->is_404 ) {
    status_header( 200 );
    $post_title               = 'change me';
    $post_content             = 'change me';
    
    $post                     = new stdClass();
    
    $post->ID                 = - 1;
    $post->post_content       = $post_content;
    $post->post_status        = 'publish';
    $post->post_title         = $post_title;
    $post->post_type          = 'generic';
    $post->post_name          = $post_title;
    $post->comment_status     = 'closed';
    $post->ping_status        = 'closed';
    $post->post_password      = '';
    
    $wp_query->found_posts    = 1;
    $wp_query->is_404         = false;
    $wp_query->is_posts_page  = 1;
    $wp_query->is_single      = 1;
    $wp_query->is_singular    = true;
    $wp_query->max_num_pages  = 1;
    $wp_query->page           = false;
    $wp_query->post           = $post;
    $wp_query->post_count     = 1;
    $wp_query->posts          = array( $post );
    $wp_query->queried_object = $post;
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question