I have created a custom table namely company Now I want to load respective company's details based on URL
I am thinking of creating URL such as example.com/?company=1 and example.com/?company=1&depart=2
Now after login employee will be redirected to its respected company URL.
Now based on company URL ($_GET['company'] )I will be setting some global vars which will contain company details such as COMPANY_NAME so that I can access on any page.
With this approach I have to maintain the URL parameters on every page load/redirect. And also maintain global vars.
Please tell me if this is a good approach or if there is any other better way. I don't want to use multisite.
I have started with using query vars
add_filter( 'query_vars', 'addnew_query_vars' );
function addnew_query_vars($vars)
{
$vars[] = 'company';
$vars[] = 'depart';
return $vars;
}
add_action( 'template_redirect', 'setVars' );
function setVars()
{
if($_GET['company']){
set_query_var( 'store',$_GET['company'] );
}else{
set_query_var( 'company',2 );
}
if($_GET['depart']){
set_query_var( 'depart',$_GET['depart'] );
}else{
set_query_var( 'depart',2 );
}
}
If parameters are set in the URL then it will get that param values else it will take the default one (i.e 2 )
Edit
I need to create URLs for each company so that I can send to clients,therefore I have to use URL parameters anyway
Well, that's an old approach, now a days, Wordpress and php even all technologies are very flexible, they provide thousands of different ways to do the same thing.
For your approach, best way is to do the things using Wordpress query param, which is a modified way to make an awesome url.
If you don't prefer to share the company ids on the url then try using php session, which is also a good practise and also you have secure data. but with session you will not be able to share the url, so best way is to use the query param.
Related
I want to create a nice readable permalink structure for my custom post type (CPT). My CPT "movie" has the following rewrite-slug movie/movie_name" (all works fine).
Now i want to add arg like this: movie/movie_name/arg and use arg in my template file as a php variable.
But obvious it lead to not-found-page. How can i achieve this target?
edit: i want it in FRIENDLY URL format, it means i dont want to use GET for this.
You may pass it like movie/movie_name?movie_arg=movie_value. It is will be available with $_GET['movie_arg']. Of course your need extra sanitization to handle this data.
To be able to read this in a WordPress way add params to a query_vars filter
function add_movie_arg_to_query_vars( $qvars ) {
$qvars[] = 'movie_arg';
return $qvars;
}
add_filter( 'query_vars', 'add_movie_arg_to_query_vars' );
Note: it should not be same as reserved WordPress query parameters
This way it will be available at your template with get_query_var('movie_arg')
print_r( get_query_var('movie_arg') ) // movie_value
More information here
I'd appreciate any advice, resources or assistance with this issue.
I want to be able to have part of my Wordpress site where I can parse the URL and then use those values to populate the page with content from another API.
For example:
server.zz/weather/Sydney%20Australia
server.zz/weather/Houston%20Texas
Where I could write a Plugin which would intercept these requests, be able to extract the end of the URLs, and then call another API to get the data to then merge into a Template to be presented to the visitor.
I know that there are Custom Post Types, but I wasn't sure if they were the best solution for this usage case.
As I said, any advice or suggestions would be appreciated.
I found the solution to this problem by using add_rewrite_rule(), add_rewrite_endpoint(), and flush_rewrite_rule().
For the example I provided earlier, I created the following code in a Plugin.
// Define the URL Rewrite Rules
function crw_rewrite_urls(){
add_rewrite_rule(
'^weather/(.+)$' ,
'index.php?weather_location=$matches[1]' ,
'top'
);
add_rewrite_endpoint('weather_location', EP_ROOT);
flush_rewrite_rules();
}
add_action('init', 'crw_rewrite_urls');
// Initialise the Query Variable
function crw_query_var( $vars ) {
$vars[] = 'weather_location';
return $vars;
}
// Check for the Variable and Display Content as needed
function crw_handler() {
global $wp_query;
if ( isset( $wp_query->query_vars['weather_location'] ) ) {
// Call the API, fill the Template here
}
return;
}
add_action('template_redirect', 'crw_handler');
I want to get the URL from where the customer can directly pay for their Invoice and also it should work with wc-cancelled and wc-transaction-declined (custom order status).
My Solution
What I'm doing now is created a custom page with my custom get parameters and processing the whole Payment Process as Documentation in Gateway provider Website.
My Problem
But the problem is whenever they update their doc file and plugin I also have to update my code; but if I get the Pay Now URL then WooCommerce and Gateway Plugin will take care of it.
Is there a better solution?
I got the solution in WooCommerce templates/emails/customer-invoice.php file. The function that I was looking for is get_checkout_payment_url().
Usage
$order = wc_get_order($order_id);
$pay_now_url = esc_url( $order->get_checkout_payment_url() );
echo $pay_now_url; //http://example.com/checkout/order-pay/{order_id}?pay_for_order=true&key={order_key}
//http://example.com will be site_url and protocol will depending upon SSL checkout WooCommerce setting.
But this url only works with pending, failed order status; So I used filter woocommerce_valid_order_statuses_for_payment
if (!function_exists('filter_woocommerce_valid_order_statuses_for_payment')) {
//http://woocommerce.wp-a2z.org/oik_api/wc_abstract_orderneeds_payment/
//http://hookr.io/filters/woocommerce_valid_order_statuses_for_payment/
// define the woocommerce_valid_order_statuses_for_payment callbackĀ
function filter_woocommerce_valid_order_statuses_for_payment( $array, $instance ) {
$my_order_status = array('cancelled', 'transaction-declined');
return array_merge($array, $my_order_status);
}
// add the filterĀ
add_filter('woocommerce_valid_order_statuses_for_payment', 'filter_woocommerce_valid_order_statuses_for_payment', 10, 2);
}
^^ I added this in my active theme's functions.php file.
Reference:
get_checkout_payment_url()
wc_abstract_orderneeds_payment
woocommerce_valid_order_statuses_for_payment
You can get url with below code but it will work for wc-pending status order only, with the help of order_id or post_id
$order = wc_get_order($order_id);
echo $pay_now_url = $order->get_checkout_payment_url();
For a plugin that I code, we use the domain name for the purpose of registering the premium licenses. We check this against our records or whatnot to ensure that they have that site registered and can therefore use the premium features of the plugin.
I'm finding, however, that when users are using WPML with different domains for each language, the registration only works on one default domain as that's the domain name being used for registration.
$domain = get_home_url();
This is essentially the function that I've been using to fetch that information.
Is there a way to fetch that url, but to always retrieve one, constant, default value regardless of which language subdomain is being loaded? Such that if they have French, Spanish, and English, and English is the default, it always returns the English domain?
Use icl_get_home_url(); function what recomends by WPML documentation.
Also, you need to check if WPML is installed and active.
You can do something like
$domain = get_home_url();
if ( function_exists('icl_object_id') ) {
$domain = icl_get_home_url();
}
If you must have the same domain returned always, use get_option( 'home' ); instead, icl_get_home_url() is deprecated and doesn't do what you are asking. At least not anymore... Currently, icl_get_home_url() returns the localized domain instead of the default, just like wpml_home_url(): https://wpml.org/documentation/support/creating-multilingual-wordpress-themes/home-page-link/
There is also WordPress Multi-site to account for though, so I'd recommend the following:
if ( is_multisite() ) {
switch_to_blog( 1 );
$url = get_option( 'home' );
restore_current_blog();
} else {
$url = get_option( 'home' );
}
This bypasses any filtering, and should ensure that you always get the primary url/domain for validation.
I have a quick question to ask.
I've setup a wordpress site with custom theme that has the functionality to set posts "Private/Public" where as you can guess all post marked as private can only be seen by users who are logged in, and public everyone can see.
How I accomplished this was using a custom field "access" and each post can set this custom field to private or public in the edit post screen. Then to display these posts I run a custom loop query with a "is_user_logged_in()" conditional statement. It that statement is true I include all posts with the "access" fields set to both "private/public" and if the statement fails ie the user is not logged in only include posts with "access" set to public. I have used similar loop queries for all single page loops etc.
Now while this works a treat I have concerns over how secure this approach is. Thats were your help comes in. How secure do you think this is? Would it be easy to trick the loop into displaying private post to a user thats not logged in? Can you reccommed a better more secure way of handling private/public posts that can be set by a select number of users on the backend?
ideas much appreciated.
Rob.
maybe I understood all wrong , but -
What You describe is just like the wordpress Default behavior for private posts .
Hence , I do not really understand wh you need a custom field for that .
Custom Fields have the habit of being [ab]used for everything, even if not needed :-)
That being said ,you can use the post_status() function to check for your status
if ( get_post_status ( $ID ) == 'private' )
{
// this is 'private';
}
else
{
// this is public 'public';
}
So you could use
get_post_status ( get_the_ID() )
or if you want to put it at the head of the loop after the the_post() part:
if( get_post_status()=='private' ) continue;
you could wrap it also with is_user_logged_in() if you want .
Point is , there is already a default place in wordpress where "private" is defined . so there is no need to define it elsewhere ( like custom field ).
You can even create your own custom post status with register_post_status() ..
the best way IMHO however , is to filter all the posts on the posts_where
add_filter('posts_where', ' privates_control');
function privates_control($where) {
if( is_admin() ) return $where;
global $wpdb;
return " $where AND {$wpdb->posts}.post_status != 'private' "; // or add your custom status
}
This function simply mofifies the query using the posts_where filter. Codex Link
You can modify it to your needs (add / remove conditions / user levels / user control