How to change the default structure generation of single post permalinks? - wordpress

I want to change the output of the_permalink() function and the likes. So, I don't want to change the mermalink structure for all the post types, only the 'post' post type, and only the single page.
I already changed the rewrite rules so that
mysite.com/news/2016/the-news-title
will drive the user to the single post page template and display content properly. Fine.
Now, as I said, I want the_permalink() function to generate links with that structure.
How could I do it?

I didn't find a proper solution but you can generate your permalink here for post type post you have $post WP_Post Object ..
Why use get_post() because post_name (permalink) is saved in this field and it will always unique.
function edit_the_permalink($permalink) {
$post = get_post(array('post_name' => $permalink));
if( $post->post_type == 'post' ) {
// Override your permalink here for post_type = `post`
echo '<pre>';print_r($post);echo '</pre>';
$permalink = '';
return $permalink;
}
}
add_filter('the_permalink', 'edit_the_permalink', 10, 1);

Ok following Noman indications, I can post my own answer. I could the 'the_permalink' filter and modify the output if post_type == 'post'. The basic code:
add_filter('the_permalink', 'edit_the_permalink', 10, 2);
function edit_the_permalink($permalink, $post) {
// you should pass the post object when calling the_permalink()
if(is_object($post) && $post->post_type == 'post'){
// [...do the magic...]
return $permalink;
}
}
Please note that both the_permalink() and get_permalink() (alias get_the_permalink()) accept the $post argument and you should pass it as a post object.
Also, note that you can do pretty much the same with the post_type_link filter if you want modify the custom post types permalink generation (output of get_post_permalink()). In this case the callback will receive 4 parameters: $post_link, $post, $leavename, $sample.

Related

Unable to save Query Vars with a post

I'm trying to save Query Vars to a post for later retrieval.
I'm using permalinks in this format: domain.com/%category%/%postname%/
Example:
I create a following page
domain.com/page-003/
I add Query Var called email to the page
add_query_arg('email', 'test#abc.com', 'domain.com/page-003/')
Now when I call
get_permalink($post_id);
I get
domain.com/page-003/
Instead of
domain.com/page-003/?email=test#abc.com
What am I missing? Aren't Query Vars saved with a post?
You want to save some meta data which you want to restore later on and add as query arg into the URL.
You need to first save it as post_meta like e.g. when you save post with that data. You use:
<?php update_post_meta(get_the_ID(), 'email_address', 'abc#mail.com'); ?>
More details: https://codex.wordpress.org/Function_Reference/update_post_meta
Then during the retrieval, you may hook into a HOOK early on like template_redirect or earlier of the post you can get post_meta to get the email and then add to query arg:
<?php $email = get_post_meta( get_the_ID(), 'email_address' ); ?>
Then
esc_url( add_query_arg( 'email', $email, get_permalink( get_the_ID() ) ) );
Something like that, code is untested, I just wrote here, you may please read detailed doc in codex for each function used above.
Update: How to update/fill Ninja form field from Meta Value:
add_filter( 'ninja_forms_render_default_value', 'wm_the_value' , 10 , 3);
function wm_the_value( $default_value, $field_type, $field_settings ) {
if( 'textbox' == $field_type && in_array('ref' , $field_settings)){
$default_value = get_post_meta(get_the_ID(),'_listing_mls', true);
}
return $default_value;
}
'ref' is field name in Ninja form.
'_listing_mls' is meta_key name from WP database.
Hope it works for you.

Show different page templates for custom post types

I am using the Custom Post Type UI plugin for worpdress and have created custom "pages". I have set these pages to be able to use the page template dropdown but would like to know if anyone knows of a way to show separate page templates for different post types?
You will need the theme_page_templates filter hook to remove the templates that you do not want to see for each post type. When the templates are loaded, including for display in the edit screen the templates that are returned are first passed to this function:
apply_filters ( 'theme_page_templates', array $page_templates, WP_Theme $this, WP_Post|null $post )
To implement you will use something like the following code:
function filter_theme_page_templates( $templates, $theme, $post ){
// make sure we have a post so we know what to filter
if ( !empty( $post ) ){
// get the post type for the current post
$post_type = get_post_type( $post->ID );
// switch on the post type
switch( $post_type ){
case 'custom-post-type':
// remove anything we don't want shown for the custom post type
// array is keyed on the template filename
unset( $templates['page-template-filename.php'] );
break;
default:
// if there is no match it will return everything
break;
}
}
// return the (maybe) filtered array of templates
return $templates;
}
add_filter( 'theme_page_templates', 'filter_theme_page_templates', 10, 3 );

update custom post custom field value when post is saved in wp-admin

How to update custom post custom field value when post is saved in admin?
I have tried to use this in misc.php for admin section:
add_action('pre_post_update', 'do_something_with_a_post');
function do_something_with_a_post($id) {
global $post;
update_post_meta($id, 'ct_Course_Dur_text_d19c', 'test12');
)
But it is not working.
You may try this (Using save_post hook), paste this code in your functions.php file
function save_cpt_metadata($id, $post)
{
if($post->post_type != 'your_custom_post_type') {
return;
}
update_post_meta($id, 'ct_Course_Dur_text_d19c', sanitize_text_field( $_POST['your_custom_field'] ) );
}
add_action('save_post', 'save_cpt_metadata');
In this example sanitize_text_field( $_POST['your_custom_field'] ) is would be actually the cstom field on your form but you may use any hard coded data, replace the your_custom_post_type with your real custom post type.

FeedWordPress - Save string as Custom Field

I am using the FeedWordPress plugin http://wordpress.org/extend/plugins/feedwordpress/ to pull posts from one site to another.
I have written a filter that after some help from Stack users successfully scans the $content and extracts the image URL into $new_content
define('FWPASTOPC_AUTHOR_NAME', 'radgeek');
add_filter(
/*hook=*/ 'syndicated_item_content',
/*function=*/ 'fwp_add_source_to_content',
/*order=*/ 10,
/*arguments=*/ 2
);
function fwp_add_source_to_content ($content, $post) {
// Use SyndicatedPost::author() to get author
// data in a convenient array
$content = $post->content();
// Authored by someone else
if( preg_match( '/<img[^>]+src\s*=\s*["\']?([^"\' ]+)[^>]*>/', $content, $matches ) ) {
$new_content .= 'URL IS '.$matches[0].'';
return $new_content;
}
else
{
}
}
What I wanted to do now was save this URL into a custom field instead of just returning it. Has anyone achieved anything similar?
So as I understand it, the plugin grabs content from external RSS feeds and creates them as posts in your website.
If this is the case, using your filter you should be able to grab the post ID within the $post variable.
So all you need is the add_post_meta() function to add a custom field to the specific post.
So including your code above it should look something like:
define('FWPASTOPC_AUTHOR_NAME', 'radgeek');
add_filter(
/*hook=*/ 'syndicated_item_content',
/*function=*/ 'fwp_add_source_to_content',
/*order=*/ 10,
/*arguments=*/ 2
);
function fwp_add_source_to_content ($content, $post) {
// Use SyndicatedPost::author() to get author
// data in a convenient array
$content = $post->content();
// Authored by someone else
if( preg_match( '/<img[^>]+src\s*=\s*["\']?([^"\' ]+)[^>]*>/', $content, $matches ) ) {
$new_content .= 'URL IS '.$matches[0].'';
//Add custom field with author info to post
add_post_meta($post->ID, 'post_author', $new_content);
return $new_content;
}
}

wp_query is empty in admin_init

I'm developing a plugin and one of the issues I am running into is that I cannot get the post id within a function assigned to the admin_init hook.
I tried a few different methods; but, they all seem to use the $wp_query.
Below is a simple version the code I am using. I implemented the code like this just now and ran it by viewing the "post edit" page
add_action('admin_init','do_optional_featured_article');
function do_optional_featured_article()
{
global $wp_query;
echo "<pre>";
print_r($wp_query);
echo "</pre>";
die();
}
$wp_query is a mostly empty array, notably, the post member is empty
-- EDIT --
I got some advice over at wordpress.stackexchange and added in this function:
function get_admin_post()
{
if( isset($_GET['post']) )
{
$post_id = absint($_GET['post']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
elseif( isset($_POST['post_ID']) )
{
$post_id = absint($_POST['post_ID']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
else
{
return false;
}
}
I think this answer will help. It states that the earliest action you can hook into, to get the global $post/$posts variables is the wp action. In the action hook reference on the codex, you can see that the wp action executes a bit after admin_init, which is why you can't retrieve any posts there, I think.
So, that should work:
add_action('wp','do_optional_featured_article');

Resources