Apache Fallback instead of add_rewrite_rule [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
My requirement is to do SEO Friendly URL for an existing site in Wordpress.
For example,
Current URL: http://localhost/test/?pid=19971
that is the page "test" is rendering the data from some custom plugin.
My new SEO URL : http://localhost/brand/sub_category/product_name
Here, Brand, Sub_category and product_name is fetched from the pid..
I am populating the data using the following rewrite rule from functions.php,
add_rewrite_rule(MY_DYNAMICALLY_POPULATED_URL.'$ page-test.php?brand='.$brand_name.'&sub='.$sub_category.'&pname='.$product_name.' [L]', 'top');
In this code, the page-test.php is placed in the root directory and the data is rendered from that file by passing the brand_name, sub_category and productname to fetch the pid. So based on the pid each product page is rendered.
This code is working fine for me, When I save the data automatically the data is written in .htaccess and page is rendering with the new SEO URL.
But when my client needs it to do it by FALLBACK in Apache instead of add_rewrite_rule due to some load balancing issue.
So can anyone help me how to do the same with Fallback resource.

This fixed my issue.
function my_query_vars($vars)
{
$my_vars = array( 'PARAMETERS' );
return array_merge($my_vars, $vars);
}
add_filter('query_vars', 'my_query_vars');
function my_rewrite_rules($rules)
{
global $wp_rewrite;
$my_page = 'PAGESLUG';
$my_rule = array( 'URL/?' => 'index.php?pagename=' . $my_page . '&school_type=$matches[1]' );
return array_merge($my_rule, $rules);
}
add_filter('page_rewrite_rules', 'my_rewrite_rules');

Related

How To Pass php Variable To another php file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have created a custom router. For Example
$istek = $_SERVER['REQUEST_URI'];
switch ($istek) {
case '/' :
$title = "Anasayfa";
require base . 'views/index.php';
break;
So I want to pass the example id variable with $_GET. But the router blocks it and redirects to the 404 page. how can I pass this variable without getting any errors or redirecting to 404?
The reason is that $_SERVER['REQUEST_URI'] includes everything, not just the / so your switch will fail.
To fix, use parse_url, / is the PHP_URL_PATH, whilst ?id= would be in PHP_URL_QUERY.
So you could use something like this to get what you want from $_SERVER['REQUEST_URI']:
<?php
// example
$_SERVER['REQUEST_URI'] = '/controller/action?id=123';
$uri = $_SERVER['REQUEST_URI'];
$uri = parse_url($uri);
$route = explode('/', ltrim($uri['path'], '/'));
print_r($route);
parse_str($uri['query'], $query);
print_r($query);
Result:
Array
(
[0] => controller
[1] => action
)
Array
(
[id] => 123
)
https://3v4l.org/UMFQF

URL showing unnecessary post types in WordPress [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
My website, let's call it example.com, has a few Post Types:
- Post (the default post type) with the slug 'blog'
- Cases with the slug 'cases'
- Klanten with the slug 'klanten' (Dutch word for clients)
When I go to the blog page, the url shows like this:
example.com/blog/
And when I then navigate to a blog post, it shows this:
example.com/blog/blog-title/
This is also good.
But now the problem. When I go the the cases page, the URL shows like this:
example.com/cases/
This is fine, but when I select a case, the URL shows like this:
example.com/blog/cases/case-title/
Ofcourse, I want this URL to be:
example.com/cases/case-title/
Try to call register_post_type() param 'rewrite' with additional argument 'with_front' => false
From Codex documentation:
'with_front' => bool Should the permalink structure be prepended with
the front base. (example: if your permalink structure is /blog/, then
your links will be: false->/news/, true->/blog/news/). Defaults to
true
$args = array(
/*other args*/
'rewrite' => array('slug' => 'cases', 'with_front' => false),
/*other args*/
);
register_post_type( 'cases', $args );

How to redirect a user with specific "ROLE" to a specific page after login in Wordpress [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have created a new user role named student_role I want to redirect the user with this role to form page(which I created from wp front end) when he logs in.
I tried peter login redirect plugin but failed.
Try this :
function rohil_login_redirect_based_on_roles($user_login, $user) {
if( in_array( 'student_role',$user->roles ) ){
exit( wp_redirect('Your page link goes here' ) );
}
}
add_action( 'wp_login', 'rohil_login_redirect_based_on_roles', 10, 2);
Explanation
If you look at the codex, you will find that wp_login provides two parameter: 1) $user_login which will return a string and 2) $user will return an object containing all the details.
But you need to make sure that you also pass priority to that parameters otherwise It will give you an error.
If you want to see what data are into that parameter, you can run below code for learning purpose only.
function rohil_login_redirect_based_on_roles($user_login, $user) {
var_dump($user); // Will give you an object
var_dump($user_login); //Will give you a string
die();
}
add_action( 'wp_login', 'rohil_login_redirect_based_on_roles', 10, 2);
Make sure you delete above code from functions.php after checking what data it contains
So as you can $user object contains roles which shows current logged in user's role. So I simple checked that if($user->roles[0] === 'student_role') if current logged in user is having student_role then wp_redirect('Your page link goes here') redirect them to some page.
Let me know if you have any doubt.

(Smarty) Function preg_match or another best way [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
What is the best way in smarty to create a function to parse a name?
example:
I have the variable {$attachment.filename}
If the file name is .jpg|.jpeg|.gif|.png
--- my result --- (option to open in lightbox... else no option to open in lightbox...)
Thanks!
Do the parsing in PHP and set a flag in a Smarty variable. In the template check the flag and display what you need. Don't embed logic in templates.
If you really need this functionality in many templates you can, indeed, write a Smarty plugin that suits your needs.
The documentation about Smarty plugins includes examples that can inspire you.
For example:
// Define the function that implements the plugin
function my_special_img($params, Smarty_Internal_Template $template)
{
$src = $params['src'];
// Based on $src decide here if you want lightbox or not
$lightbox = TRUE; // or FALSE
// Generate the <img> element
// Get information from $params, put default values, do whatever you want
$output = implode(' ', array( // build the string from pieces
'<img',
'src="{$src}"',
'width="{$params['width']}"',
'height="{$params['height']"',
'alt="{$params['alt']}"',
'class="{$params['class']}"',
($lightbox ? 'rel="lightbox"' : ''),
'/>'
));
// Smarty will replace the function call in the template with the value it returns
return $output;
}
// Register the plugin in Smarty
$smarty->registerPlugin('function', 'image', 'my_special_img');
In the templates, replace
<img src="filename.jpg" width="40" alt="bla-bla" etc>
with
{image src="filename.jpg" width="40" alt="bla-bla" etc}
That's all. Express your creativity in the plugin's code but keep it simple and use only the values provided in $params.

CakePHP help in blog tutorial - Simple Authentication and Authorization Application Last Part [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
In the Blog Tutorial on Cakephp 2.0 in the Simple Authentication and Authorization Section there is a code block in the last part that I can't understand
what does this line do?
// app/Model/Post.php
public function isOwnedBy($post, $user) {
return $this->field('id', array('id' => $post, 'user_id' => $user)) === $post;
}
I hope anyone can help me into this.
Leonardo is right, it checks if
there is a post with id $post AND user_id = $user
if yes it checks if the returned id is equal (even in type) to $post
if yes it returns true, else false
It's explained in the docs
In your case, it retrieves the Post's id field and try to match with the conditions in the second parameter: array('id' => $post, 'user_id' => $user) === $post.
This is:
find in the table Postsa row(s) with this user and this post
It will return the field in case a match is found, and false otherwise

Categories