you can use this code
add_action('init','random_add_rewrite');
function random_add_rewrite() {
global $wp;
$wp->add_query_var('random');
add_rewrite_rule('random/?$', 'index.php?random=1', 'top');
}
add_action('template_redirect','random_template');
function random_template() {
if (get_query_var('random') == 1) {
$posts = get_posts('post_type=post&orderby=rand&numberposts=1');
foreach($posts as $post) {
$link = get_permalink($post);
}
wp_redirect($link,307);
exit;
}
}
ANd then use this for random redirect
<a href="http://.....com/?random=1/" >Random Post</a>
Related Posts:
- How do I create a custom add media button modal?
- AJAX on Front-End Button Click not working – Custom Plugin
- adding a button to the media uploader
- Skewed / slanted button within elementor [closed]
- Convert to shortcode?
- Disable submit button order
- Generate Own Custom CSS Button with icon
- Share buttons Facebook Twitter, just like wordpress.com without webservice
- Upload PlugIn Button is missing
- How can I change page content for good once a button is pressed?
- How to execute certain code on back-end button press?
- Add Cancel Button to a Custom Meta Box
- how to send two forms with one click (script ninjaforms id)
- “Listen” button on my main post page
- hide load more button if there are no posts left to display
- Add Flatr button to Sharing option
- WordPress Settings – Custom Button actions
- Change text “My Basket” in woocommerce artificer theme
- Who are the most trusted plugin developers? [closed]
- Hook if somebody saves plugin options?
- Change success message in plugin Theme my login
- How should I use register_setting, add_settings_section, & add_settings_field in my plugin’s options page?
- login to wordpress with Get variables instead of Post
- Help With A Reverse Pagination Plugin
- How to update widget_text in the wp_options table
- plugin for wp_list_categories with posts
- Integrating WP as a CMS with a Bug Tracker
- Protecting against malicious code in WordPress plugin updates
- wp_specialchars and wp_specialchars_decode in a shortcode plugin
- Display All Non-Used Plugins
- Where should I add the necessary code to add an extra attribute to the kses allowed tags array?
- Display price range on woocommerce product variations
- How do I turn a custom post type into a plugin?
- How to remove xmlns on language_attributes()?
- Plugin won’t activate – cannot declare class (already in use)
- How do I prevent the link from using url of the image in Block Editor?
- Does anyone know a plugin that turns a ‘post’ into an html newsletter?
- Search Everything plugin integration
- Does WP identify plugin by plugin name or plugin_basename?
- Guidelines / restrictions about advertising /donate begging unside wordpress plugins or themes
- Working with .po and .mo files translation
- Redirecting a buddyboss profile tab to a different tab if not subscribed to a specific membership
- WordPress automatically removes line break
- Modifying Author Link to add Author Meta in URL
- How to disable a plugin for certain page?
- How to create a custom post type that contains another custom post type?
- WP Query. Is there a maximum size?
- How to Create a Custom Slug for Tags and Categories with a prefix or suffix?
- Add action to fire when a published post is updated
- How to make multiple admin pages for one plugin?
- WP Google Fonts question [closed]
- Default plugin config to override wp_options?
- Performance impact with inactive plugins? [duplicate]
- Timed content and flagging content as viewed by user?
- Amazon.com intergration with WordPress?
- Conditional Shipping Options if Certain Products are in Cart WooCommerce
- get_edit_post_link() not working on wp-cron
- WordPress won’t allow for updates to plugins or WordPress Core
- Easy WP SMTP plugin test email failed
- How to modify or create custom contact form 7 select options [closed]
- I want to generate the pdf and save it in library?
- Classic, but puzzling “The uploaded file could not be moved”
- Restrict access to live site from public while showing small group the new site all on the same URL?
- Table is not created in WAMP
- Set domain level rel canonical with Yoast SEO [closed]
- Override methods in Yoast SEO breadcrumb class
- Jetpack: Subscribe via e-mail [closed]
- How display specific category post to specific users?
- BuddyPress admin bar links are broken [closed]
- Looking for a filter to modify the months
- How to proper escape echo inside a javascript tag
- How do I reinit WordPress plugins dynamically using jS?
- why does an empty get_the_excerpt change get_the_ID to default homepage?
- Hidden permalink different from displayed permalink
- content copy protection in wordpress
- WordPress UPDATE queries on MySQL database stuck
- wp_set_password() does not work!
- Unset plugins on front-end belonging to specific category
- WordPress appearance button
- Is it a good practice to use wordpress database to store pages contents which loading to page dynamically?
- Load font awesome by content in css
- Else If statement for ACF [closed]
- how to hide resume from selected employer [closed]
- How to add styling and script to a custom widget plugin
- Where do the section input fields values are stored in database
- Link in navigation menu send user to different page if mobile?
- WordPress menu and plugin installation problem
- How to customize ‘recurrance’ of’ wp_schedule_event’?
- Call/Run a Mathematica (.m) Shell Script from WordPress Page/Blog
- CPT-onomies CPT_TAXONOMY->get_terms extremely slow with 6K posts
- ‘all’ hook and add_action issue (class based plugin)
- Embed YouTube, login to buy & payment methods
- Duplicate use of WordPress plugin
- Call current post parameters inside a plugin
- rss feed code help
- How to add/manage Followers (email subscribers) via API?
- Woocommerce where to set the Price Suffix [closed]
- How to modify submit button on GravityForms
- Detect if user is on the specific page in WordPress
- A multi-section WordPress store [closed]