Asked  1 Year ago    Answers:  5   Viewed   10 times

Say I have a Page "foo" in wordpress such that http:/www.blah.com/foo brings it up.

Say this page has a trigger within its content such as <!-- foo --!> which is being tracked by my plugin through the add_filter('the_content', ..) filter. So, all calls to http:www.blah.com/foo and http://www.blah.com/foo?var=bar are picked up by the plugin and dealt with.

Now is there a way by which the plugin could let wordpress know that it wants to handle all urls of the type http://www.blah.com/foo/bar http://www.blah.com/foo/bar/morefoo etc.. "without" having to create pages/subpages for each of these as these urls would be created dynamically?

Also, is there anyway besides using the add_content filter on the_content within a page that one can grab control from within a plugin ideally keyed of the url so all calls to http://www.blah.com/foo are handled by the plugin.

 Answers

5

Answering my own q:

The pointers in the answers above were useful and got me going on the right track, but I kept hitting a snag in that whenever the url in question was invoked it kept calling index.php.

I then came across http://mikeschinkel.com/blog/restful-web-services-in-wordpress-plugin/ where he goes into an explanation and provides an answer that requires a template_redirect action to ensure that control is where you want it.

Saturday, May 29, 2021
 
exxed
 
2

I don't know if there is a default feature for this or not but I have an idea as how to achieve it.

You can create a custom template for this and in that template get the query string by get_query_var() and then use WP_Query cat and tag argument to fetch your posts belonging to that cat/tag.

Here is a sample working code:

$cat_id = get_query_var('category');
$tag_id = get_query_var('tag');
$args = [
    //...
    //...
    'post_type' => 'post', //<-- Replace it with your custom post_type
    'post_status' => 'publish',
    'tag_id' => $tag_id, //replace tag_id with tag if you are passing tag slug
    'cat ' => $cat_id //replace cat with category_name if your are passing category slug
    //...
    //...
];

// The Query
$query = new WP_Query($args);
if (!empty($query->posts))
{
    //print_r($query->posts);
    foreach ($query->posts as $post)
    {
        //Your filtered post
    }
}

Hope this helps!

Thursday, April 1, 2021
 
TMichel
 
2

Thanks for the input.

I've had to use a 'work around' solution because it just doesn't seem to want to work.

I tried using $wp_query->query_vars to get the querystring data as opposed to $_GET or $_POST like so:

if(isset($wp_query->query_vars['major'])) {
    $program = urldecode($wp_query->query_vars['major']);
}

As suggested on: http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ but the query_var for 'major' kept being non existent even though the pagename query var from the same URL rewrite did exist.

So I moved on...

Here is the new URL rewrite:

function add_rewrite_rules($aRules) {
  $aNewRules = array('([A-Z]{4})/outline/?$' => 'index.php?pagename=programs&major=$matches[1]');
  $aNewRules2 = array('([A-Z]{4})/?$' => 'index.php?pagename=programs&major=$matches[1]');
  $aRules = $aNewRules + $aNewRules2 + $aRules;
  return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules');

Since my URL rewrite is still keeping the program code in the URL just in a prettier way, I'm simply grabbing the REQUEST_URI and parsing out the program code like so:

$parts = explode('/',$_SERVER['REQUEST_URI']);
if (isset($_GET['major'])) {
  $program = $_GET['major']; 
} elseif ($parts[1] == 'devprograms') {
  $program = $parts[2];
} else {
  $program = get_the_title();
}

This accounts for both link styles /devprograms/BUSN and /devprograms/programs?major=BUSN

The only downside to this is I cannot have other pages on the install with 4 letter names such as 'test' or 'page' as they will get rewritten to the programs page with 'test' or 'page' as the program code, which won't be actual programs. That is an issue that is easy to work around when naming pages though.

Thanks, Thomas

Thursday, April 1, 2021
 
4

This code solved the logic

function plugin_sortcode($contents) {
    global $SORTCODES, $DATA;

    foreach ($SORTCODES as $name => $fn) {
        $attr = array();
        preg_match_all("/[" . $name . " (.*?)]/", $contents, $matches);
        if ($matches[0]) {

            $code = $matches[0][0];
            if (isset($matches[1])) {
                $attrs = $matches[1][0];
                $attrs = explode(" ", $attrs);
                foreach ($attrs as $values) {
                    $attrs1 = explode("=", $values);
                    $attr[$attrs1[0]] = $attrs1[1];
                }
            }

            $data = $fn($attr, $DATA);
            $contents = str_replace($code, $data, $contents);
        }
    }

    return $contents;
}
Saturday, May 29, 2021
 
Wookai
 
3

It turns out that I did in fact have all anchor links in the page bound to an event listener and being overridden. Since that code was fundamental to the way the page worked I did not want to mess with it. Instead I bypassed it by using ng-click to call the new url as follows:

HTML:

<a class="navLinkHcp" href="{{hcpurl}}" title="Habitat Conservation Plan" target="_blank" ng-click="linkModelFunc(hcpurl)">Habitat Conservation Plan</a>

Controller:

$scope.hcpurl = 'http://eahcp.org/index.php/about_eahcp/covered_species';

$scope.linkModelFunc = function (url){
  console.log('link model function');
  $window.open(url);
}

And voila! Good to go. Thanks again to KevinB for cluing me in that this was probably the issue.

Thursday, November 4, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :