At the moment I have the following:
a custom template: my_custom_template.php where the function below is called.
<?php $uni_data = get_uni_data() ?>
a page: www.website/my_page which is using the custom_template
functions in functions.php:
function get_uni_data() {
$segments = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
$uni_data = [];
// for www.example/user/account you will get 'user'
global $wpdb;
$uni_data = $wpdb->get_row("SELECT * FROM university_table WHERE name LIKE 'Clemson'", ARRAY_A );
return $uni_data;
I want to change the query statment value after LIKE to be whatever the end of the url parse is i.e when someone goes to www.website/university/Clemson or www.website/university/Duke
the query value is Clemson or Duke.
So, I would like to have add a rule to where whenever the user goes to the url: www.website/university/NAME
the template is called and filled in using get_uni_data with NAME as the query value.
Do I need to have a page like my_page above to handle this? Or can i just use the template? How would the rewrite rule look to just the template and not with a specific page in mind?
EDIT 1
I have added the following function (as per similar question) but the page does not exist.
add_action('init', function() {
$url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
$templatename = 'university';
$pos = strpos($url_path, $templatename);
if ( $pos != false) {
$load = locate_template('my_custom_template.php', true);
return $load;
}
}) ;
EDIT 2
I will try now how this question is answered where the rewrite rule will look like:
add_rewrite_rule(
'university/([^/]+)/?$',
'index.php?pagename=university&uniname=$matches[1],
'top'
);
and the queryvars is:
$query_vars[] = 'uniname';
and change the get_uni_data function to have an argument (uniname) which will be put after the LIKE statement.
But I really do not understand how add_action is working or add_filter so I am not sure if these will work.
EDIT 3
So I have implemenetd the following in my functions.php file:
add_action('init', 'wp_university_full_rewrite');
function wp_university_full_rewrite() {
add_rewrite_rule(
'university/[^/]*)/?$',
'index.php?pagename=university&uni_name=$matches[1]',
'top');
}
add_filter('query_vars', 'wp_university_full_query_vars');
function wp_university_full_query_vars( $query_vars ) {
$query_vars[] = 'uni_name';
return $query_vars ;
}
function get_uni_data_with_query( $uni_name_query ) {
$uni_data = [];
global $wpdb;
$uni_data = $wpdb->get_row("SELECT * FROM university_table WHERE name LIKE ".$uni_name_query, ARRAY_A );
return $uni_data;
}
and in my template I have added:
<?php $uni_name_pull = get_query_var('uni_name') ?>
<?php $uni_data = get_uni_data_with_query($uni_name_pull) ?>
but now I get the following error:
Warning: preg_match(): Compilation failed: unmatched parentheses at offset 17 in /home4/ratemyp9/public_html/wp-includes/class-wp.php on line 224
At the moment I have the following:
a custom template: my_custom_template.php where the function below is called.
<?php $uni_data = get_uni_data() ?>
a page: www.website/my_page which is using the custom_template
functions in functions.php:
function get_uni_data() {
$segments = explode('/', trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'));
$uni_data = [];
// for www.example/user/account you will get 'user'
global $wpdb;
$uni_data = $wpdb->get_row("SELECT * FROM university_table WHERE name LIKE 'Clemson'", ARRAY_A );
return $uni_data;
I want to change the query statment value after LIKE to be whatever the end of the url parse is i.e when someone goes to www.website/university/Clemson or www.website/university/Duke
the query value is Clemson or Duke.
So, I would like to have add a rule to where whenever the user goes to the url: www.website/university/NAME
the template is called and filled in using get_uni_data with NAME as the query value.
Do I need to have a page like my_page above to handle this? Or can i just use the template? How would the rewrite rule look to just the template and not with a specific page in mind?
EDIT 1
I have added the following function (as per similar question) but the page does not exist.
add_action('init', function() {
$url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
$templatename = 'university';
$pos = strpos($url_path, $templatename);
if ( $pos != false) {
$load = locate_template('my_custom_template.php', true);
return $load;
}
}) ;
EDIT 2
I will try now how this question is answered where the rewrite rule will look like:
add_rewrite_rule(
'university/([^/]+)/?$',
'index.php?pagename=university&uniname=$matches[1],
'top'
);
and the queryvars is:
$query_vars[] = 'uniname';
and change the get_uni_data function to have an argument (uniname) which will be put after the LIKE statement.
But I really do not understand how add_action is working or add_filter so I am not sure if these will work.
EDIT 3
So I have implemenetd the following in my functions.php file:
add_action('init', 'wp_university_full_rewrite');
function wp_university_full_rewrite() {
add_rewrite_rule(
'university/[^/]*)/?$',
'index.php?pagename=university&uni_name=$matches[1]',
'top');
}
add_filter('query_vars', 'wp_university_full_query_vars');
function wp_university_full_query_vars( $query_vars ) {
$query_vars[] = 'uni_name';
return $query_vars ;
}
function get_uni_data_with_query( $uni_name_query ) {
$uni_data = [];
global $wpdb;
$uni_data = $wpdb->get_row("SELECT * FROM university_table WHERE name LIKE ".$uni_name_query, ARRAY_A );
return $uni_data;
}
and in my template I have added:
<?php $uni_name_pull = get_query_var('uni_name') ?>
<?php $uni_data = get_uni_data_with_query($uni_name_pull) ?>
but now I get the following error:
Warning: preg_match(): Compilation failed: unmatched parentheses at offset 17 in /home4/ratemyp9/public_html/wp-includes/class-wp.php on line 224
Share
Improve this question
edited Apr 18, 2020 at 18:01
Adam Kit
asked Apr 18, 2020 at 14:38
Adam KitAdam Kit
215 bronze badges
1 Answer
Reset to default 0Found the solution:
functions.php:
add_action('init', 'wp_university_full_rewrite');
function wp_university_full_rewrite() {
add_rewrite_rule(
'university/([^/]*)/?$',
'index.php?pagename=university&uni_name=$matches[1]',
'top');
}
add_filter('query_vars', 'wp_university_full_query_vars');
function wp_university_full_query_vars( $query_vars ) {
$query_vars[] = 'uni_name';
return $query_vars ;
}
function get_data_with_query( $name_query ) {
$data = [];
global $wpdb;
$data = $wpdb->get_row("SELECT * FROM table WHERE name = '".$name_query."'", ARRAY_A );
return $data;
}
custom_template.php:
<?php $name_pull = get_query_var('uni_name') ?>
<?php $uni_data2 = get_data_with_query($name_pull) ?>
and there is a page with slug 'university' that has its template as my_custom_template.
I know this is a very crude way of doing things, so now it's time to wrap everything in if statements and error pages!!