WordPress Rewrite Plugin - php

Example Code:
function flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function activate()
{
global $wp_rewrite;
createRewriteRules( $wp_rewrite );
flush_rewrite_rules();
}
function createRewriteRules( $rewrite )
{
global $wp_rewrite;
$new_rules = array( 'option/(.+)' => 'index.php?option=' . $wp_rewrite->preg_index(1) );
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules; // ERROR HERE............
return $wp_rewrite;
}
add_action( 'generate_rewrite_rules', createRewriteRules );
register_activation_hook( file, activate );
Sometimes gives Fatal error: Unsupported operand types, but activates the plugin and doesn't stop it from working... What am I doing wrong here?

Probably either $new_rules or $wp_rewrite->rules is not an array at the moment you want to combine them.
You could add a test to see if they are arrays, and if not, initialize them as an empty array.
(Addition:
Why pass $rewrite as an argument, when you use a global to get the original variable?)

Related

Wordpress rewrite_rules can't access second and third add_rewrite_tag

I accepted a new challenge making a custom Wordpress plugin.
After doing some research I started creating clean urls for the plugin and collected the code below.
When I visit the url https://mysite/estates/list/test/baarle/
The first custom var/rewrite_tag "jebamodus" is displayed with a value "list" in the $wp_query->query_vars array. This one's good.
The second and/or third var/rewrite_tag isn't displayed/registered?
Do I need to init each var/rewrite_tag with rewrite url seperated or are there some faults in my code?
I feel like I almost got it to work, but can't find what's wrong.
Some fresh eyes needed.
function wpse_205120_query_vars( $qvars ) {
$qvars[] = 'jebamodus';
$qvars[] = 'jebatest';
$qvars[] = 'jebaregion';
return $qvars;
}
add_filter( 'query_vars', 'wpse_205120_query_vars' );
add_action( 'init', function() {
add_rewrite_tag( '%jebamodus%', '([^/]+)' );
add_rewrite_tag( '%jebatest%', '([^/]+)' );
add_rewrite_tag( '%jebaregion%', '([^/]+)' );
add_rewrite_rule( '^estates/([^/]+)', 'index.php?jebamodus=$matches[1]', 'top' );
add_rewrite_rule( '^estates/([^/]+)/([^/]+)', 'index.php?jebamodus=$matches[1]&jebatest=$matches[2]', 'top' );
add_rewrite_rule( '^estates/([^/]+)/([^/]+)/([^/]+)', 'index.php?jebamodus=$matches[1]&jebatest=$matches[2]&jebaregion=$matches[3]', 'top' );
flush_rewrite_rules(true);
} );
add_filter('template_include', function($template){
global $wp_query;
if( isset($wp_query->query_vars['jebamodus'])) {
echo "<pre>";
print_r($wp_query->query_vars);
wp_die();
}
return $template;
});

Set #Wordpress Permalink with PHP with Conditions

What im trying to accomplish is set different wordpress permalink for logged in users
For logged in users use: /loggedin/%post_id%/%postname%/ and for others use /post/%post_id%/%postname%/
Here is the PHP code which Im trying but not working
add_action( 'init', 'smartest_set_permalinks' );
function smartest_set_permalinks() {
global $wp_rewrite;
if(is_user_logged_in) {
$wp_rewrite->set_permalink_structure( '/loggedin/%post_id%/%postname%/' );
} else {
$wp_rewrite->set_permalink_structure( '/post/%post_id%/%postname%/' );
}};
What am i missing here can somebody point out or fix this?
You were missing the '()' after is_user_logged_in. is_user_logged_in() is a default wordpress function. And the semicolon in the end of function brace is not needed.
add_action( 'init', 'smartest_set_permalinks' );
function smartest_set_permalinks() {
global $wp_rewrite;
if(is_user_logged_in()) {
$wp_rewrite->set_permalink_structure( '/loggedin/%post_id%/%postname%/' );
} else {
$wp_rewrite->set_permalink_structure( '/post/%post_id%/%postname%/' );
}
}

wp_enqueue_script not working when dependent on query string value in custom template.

So for some reason wp_enqueue_script does not execute when dependent on query string value. If I change function check to simply return true, it works. Why is this? Looking at the WordPress initiation order, wp_enqueue_script fires after parse_query which means it must be available. My goal is to only load the scripts if template foo is requested.
class Car {
public function __construct() {
if ( $this->check() ) {
add_action('template_include', array( $this, 'get_template') );
// Does not work
add_action( 'wp_enqueue_scripts', array( $this, 'get_scripts') );
add_action( 'wp_enqueue_scripts', array( $this, 'get_styles') );
}
// Works
add_action( 'wp_enqueue_scripts', array( $this, 'get_scripts') );
add_action( 'wp_enqueue_scripts', array( $this, 'get_styles') );
}
public function check() {
return ( isset( $_GET['foo'] ) && $_GET['foo'] == true );
}
public function get_template() {
return locate_template( array( 'foo.php' ) );
}
}
$car = new Car();
I suspect that it's ignoring your 'foo' parameter because to use custom query vars, they need to be registered with WordPress, using the 'query_vars' filter.
Like this:
function themeslug_query_vars( $qvars ) {
$qvars[] = 'foo';
return $qvars;
}
add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 );

wordpress: how to set up a custom URL AJAX API

I'm trying to implement a custom JSON API upon Wordpress, like /api3/get_posts, I refer to the implementation of plugin JSON-API, but it just doesn't work. Below is my code:
$myPluginBase = 'api3';
function my_plugin_init() {
global $wp_rewrite;
add_filter('rewrite_rules_array', 'my_plugin_rewrites');
add_action('template_redirect', 'my_plugin_template_rewrite');
$wp_rewrite->flush_rules();
}
function my_plugin_template_rewrite(){
global $myPluginBase;
if( isset( $_REQUEST[ $myPluginBase ] ) ){
echo $_REQUEST[ $myPluginBase ];
exit;
}
}
function my_plugin_activation() {
// Add the rewrite rule on activation
global $wp_rewrite;
add_filter('rewrite_rules_array', 'my_plugin_rewrites');
$wp_rewrite->flush_rules();
}
function my_plugin_deactivation() {
// Remove the rewrite rule on deactivation
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function my_plugin_rewrites($wp_rules) {
global $myPluginBase;
if (empty($base)) {
return $wp_rules;
}
$my_plugin_rules = array(
"$myPluginBase\$" => "index.php?{$myPluginBase}=info",
"$myPluginBase/(.+)\$" => "index.php?{$myPluginBase}=\$matches[1]"
);
return array_merge($my_plugin_rules, $wp_rules);
}
// Add initialization and activation hooks
add_action('init', 'my_plugin_init');
register_activation_hook( __FILE__, 'my_plugin_activation');
register_deactivation_hook( __FILE__, 'my_plugin_deactivation');
It seems like the template_redirect action works, as I open /api3=hello, the server will send hello back. But if I try to open /api3/hello, it keeps showing me the home page.

Rewrite customly generated urls in wordpress

I'm using Wordpress, where I have used my custom designed template, so I'm using custom generated URL's in most cases.
I want my URL to look like: website_url/details/191/
I want to rewrite this to more SEO friendly. I don't know how to write rewrite rules in htaccess and could use your help.
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_filter( 'query_vars','my_insert_query_vars' );
add_action( 'wp_loaded','my_flush_rules' );
// flush_rules() if our rules are not yet included
function my_flush_rules(){
$rules = get_option( 'rewrite_rules' );
if ( ! isset( $rules['(details)/(\d*)$'] ) ) {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
// Adding a new rule
function my_insert_rewrite_rules( $rules )
{
$newrules = array();
$newrules['(details)/(\d*)$'] = 'index.php? pagename=$matches[1]&id=$matches[2]';
return $newrules + $rules;
}
// Adding the id var so that WP recognizes it
function my_insert_query_vars( $vars )
{
array_push($vars, 'id');
return $vars;
}
This is what I have added in my function.php but no change.

Categories