use random link rather than redirect where possible by adding directory buttons content to template

This commit is contained in:
Evan Mullins 2023-01-30 10:51:34 -05:00
parent 94c9428cfd
commit 0d0f058e3c
3 changed files with 66 additions and 20 deletions

View file

@ -1026,28 +1026,31 @@ function wasmo_random_add_rewrite() {
add_action('template_redirect','wasmo_random_profile_template');
function wasmo_random_profile_template() {
if (get_query_var('randomprofile')) {
$args = array(
'orderby' => 'rand',
// 'numberposts' => 1
);
$users = get_users( $args );
foreach ( $users as $user ) {
// check that user has content and is public
if (
! get_field( 'hi', 'user_' . $user->ID ) ||
'false' === get_user_meta( $user->ID, 'in_directory', true )
// 'private' === get_user_meta( $userid, 'in_directory', true ) ||
) {
continue;
}
$link = get_author_posts_url( $user->ID );
}
wp_redirect( $link, 307 );
wp_redirect( wasmo_get_random_profile_url(), 307 );
exit;
}
}
add_action( 'pre_user_query', 'wasmo_random_user_query' );
function wasmo_get_random_profile_url() {
$args = array(
'orderby' => 'rand',
// 'numberposts' => 1
);
$users = get_users( $args );
foreach ( $users as $user ) {
// check that user has content and is public
if (
! get_field( 'hi', 'user_' . $user->ID ) ||
'private' === get_user_meta( $userid, 'in_directory', true ) ||
'false' === get_user_meta( $user->ID, 'in_directory', true )
) {
continue;
}
return get_author_posts_url( $user->ID );
}
}
function wasmo_random_user_query( $class ) {
if( 'rand' == $class->query_vars['orderby'] ) {
$class->query_orderby = str_replace(

View file

@ -231,6 +231,49 @@ if ( false === ( $the_directory = get_transient( $transient_name ) ) ) {
}
}
echo $the_directory;
if ( $context === 'full' ) {
// directory buttons etc depending on context
if ( $context === 'full' ) { // main directory
?>
<section class="entry-content alignwide">
<p><a href="/login/">Create an account</a> to add your own profile.</p>
<div class="is-layout-flex wp-block-buttons">
<div class="wp-block-button has-custom-font-size" style="font-size:20px">
<a class="wp-block-button__link wp-element-button" href="<?php echo home_url( '/login/' ); ?>" style="border-radius:100px">Create a Profile</a>
</div>
<div class="wp-block-button has-custom-font-size is-style-outline" style="font-size:20px">
<a class="wp-block-button__link wp-element-button" href="<?php echo wasmo_get_random_profile_url(); ?>" style="border-radius:100px">Random Profile</a>
</div>
</div>
</section>
<?php
get_template_part( 'template-parts/content/content', 'taxonomies' );
}
}
if ( strpos( $context, 'tax-' ) === 0 ) { // taxonomy directory page
?>
<section class="entry-content alignwide">
<p><a href="/login/">Create an account</a> to add your own profile.</p>
<div class="is-layout-flex wp-block-buttons">
<div class="wp-block-button has-custom-font-size" style="font-size:20px">
<a class="wp-block-button__link wp-element-button" href="<?php echo home_url( '/login/' ); ?>" style="border-radius:100px">Create a Profile</a>
</div>
<div class="wp-block-button has-custom-font-size is-style-outline" style="font-size:20px">
<a class="wp-block-button__link wp-element-button" href="<?php echo wasmo_get_random_profile_url(); ?>" style="border-radius:100px">Random Profile</a>
</div>
</div>
</section>
<?php
}
if ( is_front_page() || $context === 'widget' ) { // for widgets (front-page and sidebar)
?>
<div class="is-layout-flex wp-block-buttons is-content-justification-center">
<?php if ( is_front_page() ) { ?>
<div class="wp-block-button has-custom-font-size is-style-outline" style="font-size:20px">
<a class="wp-block-button__link wp-element-button" href="<?php echo home_url( '/profiles/' ); ?>" style="border-radius:100px">View All Profiles</a>
</div>
<?php } ?>
<div class="wp-block-button has-custom-font-size is-style-outline" style="font-size:20px">
<a class="wp-block-button__link wp-element-button" href="<?php echo wasmo_get_random_profile_url(); ?>" style="border-radius:100px">Random Profile</a>
</div>
</div>
<?php } ?>

View file

@ -195,7 +195,7 @@ if (
<a class="wp-block-button__link wp-element-button" href="<?php echo home_url( '/profiles/' ); ?>" style="border-radius:100px">Back to the Directory</a>
</div>
<div class="wp-block-button has-custom-font-size is-style-outline" style="font-size:20px">
<a class="wp-block-button__link wp-element-button" href="<?php echo home_url( '?randomprofile=1' ); ?>" style="border-radius:100px">Random Profile</a>
<a class="wp-block-button__link wp-element-button" href="<?php echo wasmo_get_random_profile_url(); ?>" style="border-radius:100px">Random Profile</a>
</div>
</div>