Set #Wordpress Permalink with PHP with Conditions - php

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%/' );
}
}

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;
});

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.

remove_menu_page() not getting initialized within if condition

I am trying to remove some menu pages based on user role, but when I add the function inside the if condition it doesn't do anything.
function contributor_posts_action() {
if ($role == 'contributor_posts') { // contributor_posts - custom role
// echo 'here'; for testing purposes and WORKS, so it goes under the if condition
add_action( 'admin_menu', 'remove_menus_contrib' );
function remove_menus_contrib(){
remove_menu_page( 'edit-comments.php' );
remove_menu_page( 'tools.php' );
remove_menu_page( 'edit.php?post_type=directory' );
remove_menu_page( 'edit.php?post_type=city' );
} // this function doesn't get hooked
add_action( 'admin_bar_menu', 'remove_admin_bar_items', 999 );
function remove_admin_bar_items( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'new-directory' );
$wp_admin_bar->remove_node( 'new-city' );
}// this one works properly. It's for removing for admin bar.
}
}
add_action( 'admin_init', 'contributor_posts_action' );
Try pulling the remove_menus_contrib() and the add_action( 'admin_menu', 'remove_menus_contrib' ) hook function out of your contributor_posts_action() function.
Some Wordpress hooks won't work inside other (custom) functions.

global post variable not working in wordpress plugin

I understand that the Wordpress global $post may not work until a certain point because it hasnt been loaded, but Im wondering is there a workaround for this.
Basically Im building a plugin specifically for a client. At the moment the variable is showing nothing.
This code is in a plugin in the plugins folder.
Im looking to make sure it only loads javascript (Ive left out that bit) when on specific pages (selected by the user), at the moment its loading on all pages.
My other option is to do it all on a php template, but to be honest I wanted to write it as a plugin with a view to customizing it for more generic use in the future, plus I have little experience with plugins so Im trying to improve that side of things also.
function include_js() {
global $post;
print_r($post);
if(is_object($post) && $post->ID == 14 ){
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'init', 'include_js' );
EDIT: I realised my main issue is that I want to include the javascript and because of that I need wp_enqueue_script , but I can only seem to get that work if you use the init action, which happens before the loop.
After seeing your edit, try hooking to wp_enqueue_scripts instead of init. Like this:
function include_js() {
global $post;
print_r( $post );
if ( is_object( $post ) && $post->ID == 14 ) {
// do stuff
wp_enqueue_script( 'include-map', plugin_dir_url( __FILE__ ) . 'map.js' );
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
Ref: http://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts
You should test $wp_query->queried_object_id instead of $post->id:
function include_js() {
global $wp_query;
if ( $wp_query->queried_object_id == 14 ) {
// do stuff
//...
}
}
add_action( 'wp_enqueue_scripts', 'include_js' );
There is no need for any globals etc. You can simply use is_single($post) to check if you are on a specific single post and then enqueue your script. You should always always use the wp_enqueue_scripts hook to hook your function to to enqueue scripts and styles
function include_js() {
if(is_single(14)) {
// do stuff
wp_enqueue_script('include-map', plugin_dir_url( __FILE__ ) . 'map.js');
}
}
add_action( 'wp_enqueue_scripts', 'include_js', 999 );
Remeber also, always add priority when adding custom scripts/styles. See add_action( $hook, $function_to_add, $priority, $accepted_args )

Clearing Woocommerce cart on certain pages

So, found this snippet that clears the basket and it works well when added to functions.php:
function my_empty_cart(){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
add_action('init', 'my_empty_cart');
How can I modify this and make it empty the cart only when certain pages are loaded? I played around with if ( is_page( 'pageID' ) but couldn't get it working properly!
Try this,
global $post;
if($post->ID == 'something'){
add_action('init', 'my_empty_cart');
}
function my_empty_cart(){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
for more readings.
check this link it have all the page / function codes in woocommerce
Hope its works..
WordPress conditional didnt worked for me either so i did something that dont relies on WordPress conditionals:
/*empty cart if user come to homepage*/
add_action( 'init', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
if ($_SERVER['REQUEST_URI'] === '/') {
$woocommerce->cart->empty_cart();
}
}
if u want to add to certain pages u can edit the conditional exp: (didnt try it)
if ($_SERVER['REQUEST_URI'] === get_permalink('post_id'))
if the get_permalink doest work use the exact url exp: 'https://www.domain.name/post-75'

Categories