I'm developing a plugin which creates a page and inserts shortcodes as content upon plugin activation.
My goal is to have dynamic page URLs such as the following with data/content pulled from custom DB tables which will be output in the shortcodes:
domain.com/foods/{foodtype}/{foodname}
https://domain.com/foods/fruit/cherry
https://domain.com/foods/fruit/apple
https://domain.com/foods/vegetable/spinach
https://domain.com/foods/vegetable/peas
I have the following code in the plugin main file to setup rewrite rules:
function my_rewrite_rules() {
add_rewrite_rule(
'^(foods)/([^/]*)/([^/]*)/?',
'index.php?pagename=foods&foodtype=$matches[2]&foodname=$matches[3]',
'top'
);
}
add_action('init', 'my_rewrite_rules');
function add_rewrite_tags() {
add_rewrite_tag('%foodtype%', '([^&]+)');
add_rewrite_tag('%foodname%', '([^&]+)');
}
add_action('init', 'add_rewrite_tags' );
What I'm having trouble with is, using the foodtype & foodname parameters/variables in the page's Meta Title AND Heading (H1) tags.
For example:
Foods > Fruit > Cherry
Foods > Vegetable > Spinach
I'm not sure what code to use and where.
If I use the following code in the main plugin file, it will rewrite the H1 title tag, and Post Title (as seen in Pages > All Pages), but not the Meta Title tags.
function new_title($title, $id) {
$title = 'Page has id of '.$id;
return $title;
}
add_filter('the_title', 'new_title', 10, 2);
I'm guessing I should call an add_filter() hook from within one of the shortcodes with the parameters needed to generate the correct titles? Thanks in advance.