最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Reload page with a different shortcode when a user selects from a dropdown

programmeradmin2浏览0评论

I'm using WP Job Manager, and the page which lists all available jobs is delivered using a shortcode which can be customized.

so [jobs per_page="10"] is outputed in the frontend as:

<div class="job_listings" data-location="" data-keywords="" data-show_filters="true" 
data-show_pagination="true" data-per_page="10" data-orderby="featured" data-order="DESC" data-categories="">

I want to somehow add to that page a dropdown which allow visitors to select how many listings to show per page.

What I was thinking to do is to create a dropdown where when a user selects an option, the page is reloaded with a different shortcode. So for instance:

<select>
    <option value="10">10</option> // page reloaded with [jobs per_page="10"]
    <option value="20">20</option> // page reloaded with [jobs per_page="20"]
    <option value="30">30</option> // page reloaded with [jobs per_page="30"]
</select>

My question is, is it feasible to achieve this using some <select onchange="someAction">, or is there a better solution for this?

I'm using WP Job Manager, and the page which lists all available jobs is delivered using a shortcode which can be customized.

so [jobs per_page="10"] is outputed in the frontend as:

<div class="job_listings" data-location="" data-keywords="" data-show_filters="true" 
data-show_pagination="true" data-per_page="10" data-orderby="featured" data-order="DESC" data-categories="">

I want to somehow add to that page a dropdown which allow visitors to select how many listings to show per page.

What I was thinking to do is to create a dropdown where when a user selects an option, the page is reloaded with a different shortcode. So for instance:

<select>
    <option value="10">10</option> // page reloaded with [jobs per_page="10"]
    <option value="20">20</option> // page reloaded with [jobs per_page="20"]
    <option value="30">30</option> // page reloaded with [jobs per_page="30"]
</select>

My question is, is it feasible to achieve this using some <select onchange="someAction">, or is there a better solution for this?

Share Improve this question asked Jun 10, 2019 at 17:23 MARMAR 1091 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

You can create a custom shortcode wrapper that will execute the shortcode with the desired attributes, so you would use [jobs-custom] in the page:

add_shortcode('jobs-custom', 'custom_jobs_shortcode');
function custom_jobs_shortcode($atts) {
    if (is_user_logged_in()) {
         $per_page = get_user_meta(get_current_user_id(), 'jobs-per-page', true);
    }
    if (!isset($per_page) || !$per_page) {
       if (isset($_COOKIE['jobs-per-page'])) {$per_page = $_COOKIE['jobs-per-page'];}
       else {$per_page = 10;}
    }
    return do_shortcode('[jobs per_page="'.$per_page.'"]');
}

Then add an earlier action to check for a querystring value and set the user meta/cookie...

add_action('init', 'custom_set_jobs_per_page');
function custom_set_jobs_per_page() {
    if (isset($_REQUEST['jobs-per-page'])) {
        $per_page = absint($_REQUEST['jobs-per-page']);
        if ($per_page > 10) {
            if (is_user_logged_in()) {update_user_meta(get_current_user_id(), 'jobs-per-page', $per_page);}
            else {setcookie('jobs-per-page', $per_page, (time()+(7*24*60*60);}
        }
    }
}

With this in place you can add the javascript onchange function to your <select> input:

<script>function change_jobs_per_page() {
    el = document.getElementById('jobs-per-page-select');
    perpage = el.options[el.selectedIndex].value;
    href = window.location.href;
    if (href.indexOf('jobs-per-page=') > -1) {
        href = href.split('jobs-per-page=');
        window.location.href = href[0]+'jobs-per-page='+perpage;
    } else if (href.indexOf('?') > -1) {
        window.location.href = href+'&jobs-per-page='+perpage;
    } else {
        window.location.href = href+'?jobs-per-page='+perpage;
    }
}</script>
<select id='jobs-per-page-select' onchange='change_jobs_per_page();'>

The advantage of this approach is that the new per page value is persistent until changed by the user (or the user cookie expires.)

发布评论

评论列表(0)

  1. 暂无评论