I am trying to redirect my WordPress homepage to the newest article automatically.
At the moment I use a redirect suggested by Spencer Cameron
function redirect_homepage() {
if( ! is_home() && ! is_front_page() )
return;
wp_redirect( 'http://homepage.com/article1', 301 );
exit;
}
add_action( 'template_redirect', 'redirect_homepage' );
Now if I post article 2 I want the homepage to automatically connect to article 2 without me adjusting the functions.php.
I want no user to see the www.example.com but only the article, so there is always a redirect to the newest article when visiting the page.
However:
I want to have the possibility to still access www.example.com/article1 (by manually typing the url) even if there is already www.example.com/article2.
How could I achieve that goal?
The answer is in Get the ID of the latest post: do a simple query to get one post (ordered by latest by default), then grab its permalink and redirect.
Don't put this type of code in functions.php, create your own mini-plugins to do it. If you want to disable this, it's just a matter of disabling a plugin, and not of editing a file.
<?php
/* Plugin Name: Redirect Homepage */
add_action( 'template_redirect', 'redirect_homepage' );
function redirect_homepage()
{
if( ! is_home() && ! is_front_page() )
return;
// Adjust to the desired post type
$latest = get_posts( "post_type=post&numberposts=1" );
$permalink = get_permalink( $latest[0]->ID );
wp_redirect( $permalink, 301 );
exit;
}
A solution from a friend:
Replace index.php in the template folder with the following:
<?php global $query_string; query_posts($query_string.'&posts_per_page=1'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php header('Location: '.get_permalink()); ?>
<?php endwhile; ?>
Thanks for helping me out
Related
I'm trying to set up a redirect for drafted posts of the post type "Personnel", as we'd rather show a page with a custom message than a 404 page in this instance. I have added the following to functions.php (adopted from this older thread) which for some reason only works for logged-in users- otherwise, the general 404 page shows. I've also tried adding the redirect to the single-personnel.php template, but it has no effect. I'm wondering how to get this redirect working for all users (logged in or not) and/or if there's a better way to implement the redirect? Thanks for any insight here.
add_action( 'template_redirect', 'inactive_personnel_redirect', 0 );
function inactive_personnel_redirect() {
global $post;
if( ( $post->post_status == 'draft' ) && ( is_singular('personnel') ) ) {
wp_redirect( home_url() . '/about-us/inactive', 301 );
exit;
}
}
I'm using WordPress with Elementor, I want a certain page to be accessible only if it comes from a certain url. I saw from other answers in similar questions that I can use this:
add_action( 'template_redirect', 'wpse15677455_redirect' );
function wpse15677455_redirect() {
$value = ('https://mywebsite.com/quotaton/') ;
if (!is_page(555) & wp_get_referer() !== $value ) {
wp_safe_redirect( get_home_url() );
}
};
I tried using this in the function.php of the theme but it returns the error "Unable to communicate with server to check for fatal errors". I tried with all plugins deactivated except elementor but same result.
I tried without the add_action call but, despite not giving errors, it also does nothing. I can't seem to find the right place/way to use this function.
Trying out the code, I believe the problem is that you're missing a single ampersand(&) for the And operator. Also, if is_page is used to check for the "certain page", maybe the not(!) operator isn't necessary...
if (is_page(555) && wp_get_referer() !== $value ) {
function custom_redirects() {
if ( is_front_page() ) {
wp_redirect( home_url( '/dashboard/' ) );
die;
}
if ( is_page('contact') ) {
wp_redirect( home_url( '/new-contact/' ) );
die;
}
}
add_action( 'template_redirect', 'custom_redirects' );
is_page('contact') => 'contact' is page slug.
is_page(150) => '150' is page ID.
If the link that has to be redirected comes from a page, you could use a redirection plugin like this one: https://it.wordpress.org/plugins/page-links-to/
Once activated you just edit the page you want to be redirected inserting the correct link in the page links to field.
I would like to change to homepage of a wordpress website depending on the role of the user who is logged in.
For the simple case ( i.e. whether a user is logged in or not ) I have tried using the function is_user_logged_in()
the code is as below:
if(!is_user_logged_in()){
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
}else{
echo "you are logged in";
}
But the problem is that it changes the content in the each and every page. I would like to do that for a particular page only.. ( i.e. homepage ). How do i do that? Any help would be greatly appreciated.
To do it you would have to add another condition(s), e.g. for the homepage:
if(!is_user_logged_in() && is_home()){
...
}
And similarly for the other type of the content: is_page(), is_category(), is_author(), etc. Check out the docs here.
check for the Id of the page and compare it to your wanted specific page
also you could set a category for the pages you want to alter and check for that
how can i understand a short code is exist in my wordpress post?
[tbps id="1"] is the short code . Where id may vary . ie 2,3 ...
i paste this code in wordpdpress post namely mypost ,it is working .So how can i get the shortcode which exists in wordpress page (in mypost). i don't know how to use this code
<?php
if ( shortcode_exists( 'yourCondition ' ) ) {
//Here i don't know how to use 'yourCondition' .Because condition
//is varying .It may be [tbps id="1"] ,[tbps id="2"] ,[tbps id="3"] etc
}
?>
<?php
if ( shortcode_exists( 'yourCondition ' ) ) {
// The [gallery] short code exists.
}
?>
you can try
In wordpress 3.6 and above you can do this... Say your shortcode like this... [fts youtube username=GoProCamera vid_count=5]
// Detect if the shortcode exists in a post page or widget
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'fts youtube') ) {
wp_enqueue_style( 'fts_youtube_css', plugins_url( 'youtube/css/styles.css', dirname(__FILE__) ) );
}
This works even if you use multiple shortcodes on a page, post or widget, and of course will not duplicate your styles or js because you are enqueueing them. Tested and confirmed. https://codex.wordpress.org/Function_Reference/has_shortcode
Is it possible to change the Wordpress search permalink from:
www.mydomain.com/search/my+result+keyword+term
to:
www.mydomain.com/my-result/keyword-term
and to display the search page or any ideas for this.
Thank You.
Open the searchform.php within your theme folder and change the form action. You can of course even rebuild the whole search form if you like.
The wordpress codex has a usefull tutorial on this topic:
http://codex.wordpress.org/Creating_a_Search_Page
It's too late but could help others like me :)
Add following code into your functions.php file.
function wp_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
}
}
add_action( 'template_redirect', 'wp_change_search_url' );
function wp_search_rule(){
add_rewrite_rule('^search/([^/]*)?', 'index.php?s=$matches[1]', 'top');
}
add_action( 'init', 'wp_search_rule' );