• Resolved cimenta

    (@cimenta)


    Hi
    I want to code a plug-in that creates new custom home page. Note that I have never coded any plug-in before.

    1) After activation of plug-in user needs to go to the options of the plug-in and set up that current home page will be replaced by the one that this plug-in generates ( I hope I can code this bit somehow in the future )

    2) Plug-in generates custom home page that displays only

    – title and body of the latest post
    – number of comments

    Could someone help me how to code the 2) ?

    Currently I have one php file that is very simple plug-in. I can activate / deactivate it but I don’t know how to code the home page.

    Thank you

    R

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I can’t give you actual code, but I can outline the steps and give you some background.

    You can actually get the home page to only show the latest post by hooking into the ‘pre_get_posts’ filter and changing the query variables if the request is for the home page. However, the post will still be displayed according to the theme.

    To change what’s displayed, you’ll need a different template. Depending on the theme, you could probably cause your template to be loaded instead of anything else by hooking certain actions, but to ensure your plugin works with all themes, adding a page is a good approach. This can be done by by script with wp_update_post() which works for new pages as well. While you could create a new query on this page or use get_posts() to get the latest post, it’s still best to hook the ‘pre_get_posts’ filter.

    The bit about changing the current homepage can also be scripted, it’s a matter of changing some values in the options table. Exactly what I’m not sure. If you examine the table contents you should be able to discern what needs to change. Do so with update_option().

    Thread Starter cimenta

    (@cimenta)

    Thank you for your reply.

    I am not sure if I can use your suggestions …. I don’t need to create new wp page. The page will be created by the plug-in.

    When I used this code

    add_action('wp_head', 'new_front_page');
        function new_front_page(){
         if (is_front_page()){
           echo 'is_front_page():'.is_front_page();
         }
        }
      }

    I am almost where I want to be except that the theme is adding the page content too. So I need disable that somehow. Any idea?

    Thank you

    Rl

    Moderator bcworkz

    (@bcworkz)

    If you don’t want to add a page that’s fine, but you need to somehow get your plugin’s template loaded in place of the theme’s in order to control what’s displayed. Adding a page is an easy way to do this. You could try hooking the action ‘get_template_part_content’ and require_once() your template from within the action callback, but I’m not sure how to suppress the theme’s template from loading if you go this route.

    Thread Starter cimenta

    (@cimenta)

    I got is sorted out by replacing the page template via

    // Filter to replace the template of the home page
       	add_filter( 'template_include', 'replace_home_page' );
    
    	function replace_home_page( $template ) {
    	  if ( is_front_page() ) {
    	    return plugin_dir_path( __FILE__ ) . 'new_template.php';
    	  }
    	  return $template;
    	}

    Thank you for you help

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to create custom home page via plugin?’ is closed to new replies.