How to disable template files in single page wordpress theme - wordpress

I am creating a single page Wordpress site and I would like disable/delete/remove all other pages so that they just return a 404.
So if the user tries to go to an archive page or a single post page it does not work. I only want one working page on the entire site.

You can simply force all pages to be redirected to the front page by putting the following code in your functions.php:
function redirect_to_homepage() {
if ( ! is_front_page() ) {
wp_redirect( home_url( '/' ), 302 );
exit;
}
}
add_action( 'template_redirect', 'redirect_to_homepage' );
Just a quick note: is_front_page() checks if the user is trying to access the page that is set for the front page in Settings > Reading. If no such page is set, use is_home() instead.
Reference:
http://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
http://codex.wordpress.org/Function_Reference/is_front_page
http://codex.wordpress.org/Function_Reference/is_home

Instructions for what I think you want to have done. This will remove all other pages/posts, and make the page you did not delete into the home page of your website.
Log into your site
In the admin panel go to pages
Select all of the pages except the one you want to keep as the home
In the "Bulk Actions" select move to trash and click apply
Go into the trash section of pages
Select all of the pages
In the "Bulk Actions" select move to delete permanently and click apply
Repeat steps 2-7 for the "posts"
Go to the settings reading page in the admin panel
Check off static page
In the front page drop down select the page you did not delete
Click save at the bottom of the page.

Related

Woocommerce product archive displays nothing when called from custom Page

I have a woocommerce shop page which shows me all of the products perfectly. This page is also set as the "Shop" page in the settings. Now I want a second page called Home, which should use the same template as the shop page. (basically a second shop page without categories and some news)
home-template.php
<?php /* Template Name: Homepage */
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
wc_get_template( 'archive-product.php' );
?>
I'm using the standard archive-product template from woocommerce's github.
I can see the template being loaded, like the header, but certain woocommerce functions don't return the expected value. woocommerce_before_shop_loop doesn't return anything, even though it should display the orderby dropdown. woocommerce_product_loop() returns true, but wc_get_loop_prop('total') returns zero.
I've also tried renaming the file to front-page.php but that didn't help. The home page is set correctly in the reading settings.
Am i missing a call to a query, or is it something else? Thank you in advance for your help!
When WooCommerce creates the builtin shop page it saves the id of the created page in the wp_options database table with option_name 'woocommerce_shop_page_id'. When WordPress loads a page WooCommerce checks if the page id of the page being loaded is equal to the option_value of option_name 'woocommerce_shop_page_id' and then executes code to generate the HTML of the shop page. If you have looked at the shop page in the page editor you will notice that the content is empty. The magic is being done with hard coded routines that execute for this special id.
Since, your custom page has a different page id none of the custom code for generating the shop page will be execute. So, you need to execute this magic code for your custom page. It can be done but you need a good understanding of WooCommerce.
I suggest you reconsider your design and instead of a new page just customize the existing shop page using actions and filters.

Is it possible to customize the logout page or create a custom logout page in Wordpress?

At the moment if the user navigates to the default logout page it looks like this:
This is not consistent with the theme of my website so I would like this content inside my own custom page. Can this be done?
I have a plugin installed My Theme Plugin which is designed to let me specify a logout page but I do not know how to construct it.
Since this may help others, I'm adding my comments as an answer.
You need to initially use the logout_url filter - https://developer.wordpress.org/reference/hooks/logout_url/
This will allow you to set up a page when a user clicks on the logout link. Next, you simply create the page however you need to (basic WordPress page, special template, etc.).
On that page you would use wp_logout_url() to set the link for the Are you sure you want to logout text. e.g.
Logout
This would redirect the user to the home page after they've logged out.
Edit: shortcode to add to content:
function wpso58817718_add_logout_link( $atts ){
return 'Logout';
}
add_shortcode( 'logout_link', 'wpso58817718_add_logout_link' );
Then you can do [logout_link]
You'll have to update the end URL wp_logout_url( home_url() ) if you don't want it to go to the home page.
You can use below code to redirect user to your specific url after logout
add_action('wp_logout','ps_redirect_after_logout');
function ps_redirect_after_logout(){
wp_redirect( 'your url here' );
exit();
}

WordPress: why is my root page the Blog page despite this not being the case in Reading settings?

So far I had no Blog page but I have a front-page.php template called when visiting the website root. Today I created a WP "Blog" page and set the settings as shown in the screenshot below:
Now, both the Blog page and the root/home page is run by index.php, whereas I want the home page to continue being run by front-page.php. How to fix that problem?
WordPress calls the template based upon how you have the "Front page displays" configured. These settings and what is called confuses a lot of folks. Bare with as I explain.
Template Loader
WordPress sets the template to load in wp-includes/template-loader.php. It's determined by the conditionals. For front page versus home page, it's determined here:
elseif ( is_front_page() && $template = get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template() ) :
Notice that if is_front_page() conditional is set, then the template to be loaded will be the front-page.php. Else if is_home() is set, then the template is home.php. Notice the order too, as is_front_page() is first.
Front Page vs. Home Page
The front page can be:
Posts Page
or a static Front Page
Let's walk through the configuration settings to see which template is called.
Default Configuration
Out of the box, WordPress defaults to the "Front page displays" being set to "You latest posts."
With this setting, WordPress does:
Loads the front-page.php template file
is_home() is set to TRUE
is_front_page() is also set to TRUE.
Static Posts Page
When you configure a static page as the "Posts Page" but nothing for the "static Front page," WordPress does the following:
Loads the home.php template file
is_home() is set to TRUE
is_front_page() is to FALSE
This setting is telling WordPress that you want the Posts Page to be the root of your website. The Posts Page will query all of the posts for you to display.
Both Statics Configured
If you configure a static page for both the front and posts pages, then each template is called when you are on that page.
Website Root
When you are at the root of your website, http://example.com/, WordPress does:
Loads the front-page.php template file
is_home() is FALSE
is_front_page() is TRUE
On Posts Page
When you are requesting to view the posts page, e.g. http://example.com/blog, WordPress does:
Loads the home.php template file
is_home() is TRUE
is_front_page() is FALSE
For You
If you are building a theme for resale, you will want to build your theme to comply with how WordPress determines which template to load. Let the user configure what s/he wants through Settings > Reading.
If this site is for personal use, then you could write code to override WordPress and force it to load your front-page.php instead. In doing so, keep in mind that if you ever change Settings > Reading configuration, it will not perform as expected.
Forcing the Front Page Template
Let's say you are not concerned about what is set in Settings > Reading and you want to force your website to use the front-page.php template.
In this edge case, you have to check the conditionals, load the front-page.php, and then return out of home.php. You could do something like this:
<?php
/**
* Static Posts Page Template
*
* #package YourTheme
* #since 1.0.0
* #author yourname
* #link your link
* #license GNU-2.0+
*/
// Forcing this page to redirect to the front page template
if ( is_home() && ! is_front_page() ) {
require_once ( __DIR__ . '/front-page.php' );
return;
}
// else do stuff
Use this code with extreme caution, as it will be confusing to others.
Better Option
Move your code to a view file and then call it from within the appropriate template file and/or state. Then your code is modular and available for reuse and readability without the confusion.
I've already faced this issue a few times for different reasons, yours might be different, so I'll list some thoughts here about what might be causing it and hopefully how to have it solved.
Selectively, you could try the following:
Make sure you have created a new page under WordPress admin and that you have selected your front-page.php template. Search for its name under the Page Attributes sidebar, see the example image bellow:
Go back to Settings >> Reading and re-check your static front page settings, make sure to select the page you created on Step 1 for which you've assigned the right template. Save changes.
For unknown reasons, some plugins or theme's settings might mess up with your permalinks. Although this is not directly related to the static front page settings, often times this triggers strange site wide behaviors. You could try to reset your permalinks just for the peace of mind.
Go to Settings >> Permalinks and pickup any option, Save changes. If you want to revert to your previous settings, pick it up and Save changes again. After Saving changes all your permalinks will be regenerated.
If you've followed Steps 1 and 2 or 3 (optional) and your front-page.php template is ok, you'd be able to see your template served as the front page of your website.
Otherwise, if it fails, you could try to debug for any critical erros on your website.
Make sure your front-page.php has no critical errors. For debug purposes you can enable the WP_DEBUG by placing define( 'WP_DEBUG', true ); on your wp-config.php
After enabling the WP_DEBUG, visit your website to check for any erros.
Let us know if that helps. Good Luck!

How to stop showing menu in static home page wordpress

Ok, I'm creating a wordpress theme. I don't want my navigation menu to show in the home page "Only" if the site admin setup Front page displays > A static page (select below).
otherwise I want to show the menu in home page & other pages too. I've used this <?php if(!is_front_page()):?> function, but it is not working.
some one suggest me <?php if(!is_home()):?>, but it is not working either.
So how do I make it work?
Please follow the below steps:
Dashborad > Settings > Reading
Select A static page (select below) option in Front page displays section.
Select the page you want to display from Front Page drop down box.
In header.php in the code for displaying menu, make the alteration as below.
if ( ! is_page( '{slug of the selected page in Front Page drop down box}' ) ) {
//enter your code to display menu here
}
?>

Admin URL to my plugin's page

My plugin is basically a link display page, for instance if you want to display a page with links to other websites.
In wp-admin I have a menu item on the left side bar added with this code:
function bls_add_menu_page() {
add_menu_page('Custom Links', 'Custom Links', 'manage_options',
'customlinks', 'bsl_admin_page', '', 15);
}
After adding a new link, I want to redirect to my plugin home page in admin. The URL when I click on my plugin menu link is :
localhost/wp-admin/admin.php?page=customlinks
How do I get that URL in Worpdress? Currently I just do this :
wp_redirect('/wp-admin/admin.php?page=customlinks');
but I hope there is a better way of getting my plugin admin URL?
You get the concrete URL to admin.php by using the admin_url function:
admin_url('admin.php'); # http(s)://localhost/wp-admin/admin.php
That function chooses the proper sheme (http/https) based on your Wordpress configuration for you so you do not need to care about it. Same for the path to the admin. The only thing you need to specify is the file name (admin.php).
And in your concrete example you add the page query-info part:
$url = admin_url('admin.php?page=customlinks');
wp_redirect($url);
URL for menu page or options page has 'page' parameter ( page slug defined in add_menu_page() or add_options_page() ). You can always get the current page from $_GET['page'] param, so URL for the options page is:
admin_url( "options-general.php?page=".$_GET["page"] )
, and URL for menu page ( actually it works with options pages also ) is:
admin_url( "admin.php?page=".$_GET["page"] )

Resources