Using cpt and pass a variable using url - wordpress

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

Related

Custom function error to produce a shortcode resulted in white screen of death

I followed the syntax the best I could to create a shortcode on execution, I got the wsod. Once removed, all was well. But I don't know what is wrong with my code. This code sits inside 'My Custom Functions', a plugin for wp.
In researching how to write a custom shortcode, I discovered instructions here: https://torquemag.io/2017/06/custom-shortcode/ My expertise is in mysql and use mostly plugins in our wordpress website. I am very limited with coding.
function last_updated_shortcode {
$last_updated = $wpdb->get_results( "SELECT MAX(process_time) FROM
qgotv.last_updated");
return $last_updated;
}
add_shortcode( 'last_updated', 'last_updated_shortcode' );
This shortcode should retrieve a max(datetime value) from a db table so it can be displayed on a page. The query works. The qgotv db is separate from the wordpress db but can be accessed through wp.
Two issues I can see, one is that you have a syntax error in your function. When defining a function in PHP, you need to include the arguments parenthesis: function my_function(){ /* Do Stuff */ }. Also, you probably need to reference the $wpdb with the global keyword.
You can read up a bit on the $wpdb class as well as creating your own functions.
This should get you sorted out:
add_shortcode( 'last_updated', 'last_updated_shortcode' );
function last_updated_shortcode(){
global $wpdb;
$last_updated = $wpdb->get_results( "SELECT MAX(process_time) FROM qgotv.last_updated");
return $last_updated;
}

WordPress -Mainintaing URL parameters

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.

WP Shortcodes not being added

I was creating a wordpress plugin where the user enters in some information, and is able to generate shortcodes. I was not quite sure where the shortcodes are supposed to go - my current setup is class-based, and I want to be able to create a shortcode when an AJAX request is being made, and is successful. The following two methods are in the same file in the class.
This method gets called via the admin-ajax.php file:
public static function processAjax()
{
global $wpdb;
$event_data = $_POST['obj'];
$event_title = $event_data[0];
$event_subdomain = $event_data[1];
$result_events = $wpdb->get_results("SELECT * FROM wp_shortcode_plugin WHERE subdomain = '{$event_subdomain}'", OBJECT);
if (sizeof($result_events)>0) {
echo "duplicate";
} else {
add_shortcode($event_subdomain, 'getEmbed');
$results = $wpdb->insert('wp_shortcode_plugin', array("event_name"=>$event_title, "subdomain"=>$event_subdomain));
echo json_encode($_POST['obj']);
}
die();
}
And here is my getEmbed() method that I would like to call.
public static function getEmbed()
{
return 'test';
}
It seems that the shortcodes are not being created, however. Am I missing something here? Also, is it possible to pass a value to the getEmbed function from the add_shortcode() method?
Instead of adding shortcode directly from AJAX, you should use update_option to store in the information for the shortcode to be loaded. If option doesn't exist, it will be created.
Than you will simple use wp_init hook to load up all of the shortcodes you need to load in the function.php file for the theme or plugin php file.
You should use get_option within the wp_init hook and check the values in there. You will need to have function(s) associated with the shortcodes, which can be autogenerated in php using create_function or you can route them through 1 function (defined in your php file) that will have the $atts and $content parameters defined and do whatever depending on the value of your get_option that you send to that function.
add_shortcode function should be defined within the wp_init hook, after checking the value of the get_option function. You will need to give your option a name and add to it via the ajax function. the option will most likely want to be an array, that wordpress will automatically serialize. Than you use that array returned from get_option to loop through the array of shortcodes, and call add_shortcode as many times as you need there. This requires setting up your option array so that it has a shortcode tag defined in each index of the array. I would, personally, make the shortcode tag the key of the array and all attributes of the shortcode, imo, would than be an array of that array.
Hope this helps you to get started on this.

How to extract the variables out from "add_shortcode" function in Wordpress?

In Wordpress (now in Template Fiile), lets say i have a custom Shortcode function, like:
In the PAGE:
[myshortcode id='1234']
Then, in the Template File:
function parseUserID_func( $atts ){
//parse the $atts here...
$userID = 1234; //now i get the userID is, 1234
}
add_shortcode( 'myshortcode', 'parseUserID_func' );
//now .. echo the value of $userID here? <----------
printout_UserDetails( $userID );
As you can see, how can i get the processed value of $userID variable out from the shortcode function please? (as in the last line)
The $userID is not touchable from the outside.
Actually what i'm trying to do is something like:
Pass an ID from the Page, using [myshortcode id="1234"]
Parse out the id from the shortcode's function. (parseUserID_func above)
When i get the id, i need to pass it to another custom function printout_UserDetails($id){} function (written in the Template File) .. to continue another processing works.
(Something like) then i will print out the USER DETAILS on the Page, from the printout_UserDetails() function.
This is why i need to pass a value from Shortcode, then parse it, then pass the id to my another custom function.
Any help please?
From what I'm understanding you're not trying to generate output with the shortcode, but to save data instead. I would try using custom fields to handle this.
If you need to output the "USER DETAILS" block within the post content (right where the shortcode is written), you should just merge the printout_userDetials logic into the actual shortcode handler function itself (in this case parseUserID_func).

Add a posts meta-value to the permalink/url in Wordpress

I am trying to build a speaking URL for a custom post type in Wordpress storing event dates. I would like to have the following scheme for event-URLs:
/events/2013-12-24-christmas/ /events/2013-12-31-newyearseve/
The date is not the standard published date, it is instead a date stored in a meta-field: the date on which the event takes place.
I tried to achieve it with a snippet I used to rewrite another custom post type and it looked promising - but the only problem is: I can't get the post-id to query for the meta values :(
Here's the rewrite code so far:
function create_post_type_events {
[...custom-post-type...]
register_post_type('my_events', $args);
global $wp_rewrite;
$permalink_structure = '/events/2013-12-24-%my_events%';
$wp_rewrite->add_rewrite_tag("%my_events%", '([^/]+)', "my_events=");
$wp_rewrite->add_permastruct('my_events', $permalink_structure, false);
}
add_action('init', 'create_post_type_events');
The date is obviously hard-coded and needs to be replaced.
I tried $post, $wp_query, globalizing, get_queried_object()... nothing seems to help.
So, how (and is it actually possible?) can I get the post-ID for further querying?

Resources