FeedWordPress - Save string as Custom Field - wordpress

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

Related

how to change edit url with "Id" to edit url with "Slug" in Wordpress

I have this is WordPress Post editing Url:
https://example.com/wp-admin/post.php?post=ID&action=edit
and I want to change it to Slug Not the ID Like This:
https://example.com/wp-admin/post.php?post=Slug&action=edit
I was trying to edit the post with this Url but is not working:
https://example.com/wp-admin/post.php?post=MyPostSlug&action=edit
In order to change the edit post link structure, you can use the get_edit_post_link filter like so:
add_filter( 'get_edit_post_link', 'so_73914075_get_edit_post_link', 10, 3);
function so_73914075_get_edit_post_link($link, $post_id, $context) {
$post = get_post( $post_id );
if ( ! in_array( $post->post_type, array( 'post', 'page' ) ) ) {
return $link;
}
$post_type_object = get_post_type_object( $post->post_type );
if ( 'revision' === $post->post_type ) {
$action = '';
} elseif ( 'display' === $context ) {
$action = '&action=edit';
} else {
$action = '&action=edit';
}
if ( 'display' === $context ) {
$post_type = '&post-type=' . $post->post_type;
} else {
$post_type = '&post-type=' . $post->post_type;
}
$custom_edit_link = str_replace( '?post=%d', '?post-name=%s', $post_type_object->_edit_link );
return admin_url( sprintf( $custom_edit_link . $action . $post_type, $post->post_name ) );
}
This will change the edit links for regular posts and pages to something like this:
https://example.com/wp-admin/post.php?post-name=the-post-slug&action=edit&post-type=post
WARNING: make sure you limit this to only the post types you need to change the URL for. Making this change global will almost surely
have unintended effects over other plugins
However, making the admin panel actually load the edit screen is not that easy.
Looking at the source code of the wp-admin/post.php file, you will see that there is no way to hook into the flow and populate the $post_id variable with the post id matching the slug you are sending through.
That means you have 2 options:
RECOMMENDED Update the edit link to whatever format you want and then create an admin redirect function that pulls the slug from the initial URL and redirects to the actual edit page with the ?post=%d in it.
NOT RECOMMENDED Create a new admin edit page that will understand your URL structure and include the wp-admin/post.php after the code that pulls the $post_id based on the slug.
The 2nd method might come with lots of extra code you need to write to cover all the cases and all the instances a post reaches the post.php in the admin panel

wordpress replace text before showing it to the browser

My website is built using Elementor using GeneratePress as default theme and I want to replace output final text with some values from the database. This plugin
Real-Time Find and Replace https://wordpress.org/plugins/real-time-find-and-replace/ is doing the replace functionality perfectly and it uses action:
//Handles find and replace for public pages
add_action( 'template_redirect', 'far_template_redirect' );
to replace text using code:
function far_ob_call( $buffer ) { // $buffer contains entire page
$far_settings = get_option( 'far_plugin_settings' );
if ( is_array( $far_settings['farfind'] ) ) {
foreach ( $far_settings['farfind'] as $key => $find ) {
if( isset( $far_settings['farregex'][$key] ) ) {
$buffer = preg_replace( $find, $far_settings['farreplace'][$key], $buffer );
} else {
$buffer = str_replace( $find, $far_settings['farreplace'][$key], $buffer );
}
}
}
return $buffer;
}
I can not use the plugin directly because it repalce text with text and I need to fetch data from database, so I have to make my own plugin. But, I found this blog: https://markjaquith.wordpress.com/2014/02/19/template_redirect-is-not-for-loading-templates/ that tells me to use a filter:
add_filter( 'template_include', 'my_callback' );
instead of the above action in plugin:
add_action( 'template_redirect', 'far_template_redirect' );
But the plugin is using the action.
What to use?
The plugin description says: "Set up find and replace rules that are executed AFTER a page is generated by WordPress, but BEFORE it is sent to a user's browser." this is what I exactly want. That is to replace text after page is generated and just before the page is sent to browser, but the plugin uses action for this, and other blogs suggest to use filter?

Use postmeta in wordpress shortcode

Trying to get my post meta from posts using shrotcodes and then displaying it on the content.This is the code that's trying to do this:
$string = '';
$custom_content = get_post_custom($post->ID);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$content = get_the_content();
$bonus = $custom_content["bonus"];
$string .= $content . $bonus . '<br>';
endwhile;
}
return $string;
It's not working as the custom content returns empty. Whats wrong? Thanks in advance
I don't think you got the shortcode idea, you should read some info about add_shortcode() also you can use get_post_meta() to retriev the metadata from the database.
Here is an example on how you can achieve this but this will only work with the main loop (as default):
<?php
//you can put this code in functions.php or you can build a plugin for it
function metadata_in_content($attr) {
//this is the function that will be triggerd when the code finds the proper shortcode in the content; $attr is the parameter passed throw the shortcode (leave null for now)
global $wpdb, $wp_query;
//we need global $wpdb to query the database and to get the curent post info
if (is_object($wp_query->post)) {
$post_id = $wp_query->post->post_id;// here we save the post id
$metadata = get_post_meta( $post_id, $key, $single ); // here we get the needed meta, make sure you place the correct $key here, also if you don't want to get an array as response pass $single as "true"
return $metadata; // this finally replaces the shortcode with it's value
}
}
add_shortcode('insert_metadata', 'metadata_in_content');
//the above code hooks the metadata_in_content function to the [insert_metadata] shortcode
?>
Now all it's left to do is to place [insert_metadata] in the post content and things should work.

Change the page title using a short code

I am coding a plugin and I use a short code to render a page content. I want to change the page title. I used this:
// Change the title of the page of the charts to add the current month.
add_filter('the_title', 'charts_title', 10, 2);
function charts_title($title, $id) {
$title .= ' ' . date('F Y');
return $title;
}
But it does that for all the posts and pages. Can I do that only for the pages that contains the short code I created ? I tried to do that, but it doesn't work.
add_shortcode('charts-vote', 'charts_vote');
function charts_vote($atts) {
// Add the filter only in the short code function callback.
add_filter('the_title', 'charts_title', 10, 2);
// ... //
return $content;
}
Can someone please help me ?
I understand that the specifics of your set up may require checking for the Shortcode, but maybe a Custom Field could be used for that:
add_filter( 'the_title', 'charts_title_so_15312385', 10, 2 );
function charts_title_so_15312385( $title, $post_id )
{
// Admin area, bail out
if( is_admin() )
return $title;
// Custom field not set, bail out
$mod_title = get_post_meta( $post_id, 'mod_title', true );
if( !$mod_title )
return $title;
// Ok, modify title
$title .= ' ' . date( 'F Y' );
return $title;
}
The Shortcode could even "talk" with the Custom Field for extended configurations.
For ease of use, you could make a Custom Meta Box or use a plugin like Advanced Custom Fields.

Replacing the title of a post with a custom taxonomy on wordpress

Using filters/hooks, how would I replace the title of a wordpress post with whatever term has been selected from a custom taxonomy.
Hopefully the attached image will explain what I'm trying to do.
Let's say I selected 'Powerchrono' - I would like the title of the post to be replaced with the selected term, and it's parent.
Any help would be much appreciated.
I'd obviously like the url of the post to also be updated too.
I can't guarantee this will work straight out of the gate since it's untested. But this should get you started:
functions.php
<?php
add_action('save_post', 'update_term_title');
function update_term_title($post_id)
{
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
if(!current_user_can('edit_post', $post_id))
return;
//Replace 'manufacturer' with whatever your custom taxonomy slug is
$terms = wp_get_post_terms($post_id, 'manufacturer');
if(empty($terms))
return;
$title = false;
foreach($terms as $term)
{
if($term->parent)
{
$parent = get_term($term->parent, 'manufacturer');
$title = $term->name.' '.$parent->name;
break;
}
}
/*Default to first selected term name if no children were found*/
$title = $title ? $title : $terms[0]->name;
/*We must disable this hook and reenable from within
if we don't want to get caught in a loop*/
remove_action('save_post', 'update_term_title');
$update = array(
'ID'=>$post_id,
'post_name'=>sanitize_title_with_dashes($title),
'post_title'=>$title
);
wp_update_post($update);
add_action('save_post', 'update_term_title');
}
?>

Resources