Hello,
I need help with this wordpress function. Here is what i am trying to do
I currently use IFTTT.com to aggregate content and publish post to my wordpress. I would like these post to link to the source permalink of the original article. I am looking for a way to apply the source url to a custom field automatically and have that url as the post permalink.
Ideal solution would be to create a shortcode that will allow me to enter a value for a custom field. example
[customkey="source_link"]VALUE[/customkey]
1. Any text that is in value will be applied to custom field "source_link"
2. On post creation WordPress will check if custom field exist and apply it as the post permalink
can this be done or is there another alternative?
timDesain Nanang answers:
try:
put this code in the ifttt's body field (see screenshot):
[source_link] {{PostUrl}} [/source_link]
put this code in the theme's functions.php
//saving post meta
add_action( 'save_post', 'timd_save_meta' );
function timd_save_meta( $post_id ) {
$get_post = get_post( $post_id );
$post_content = $get_post->post_content;
//based on get_shortcode_regex function
$pattern = '\[(\[?)(source_link)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)';
preg_match('/'.$pattern.'/s', $post_content, $matches);
if (is_array($matches) && $matches[2] == 'source_link') {
$meta_value = esc_url(trim($matches[5]));
update_post_meta( $post_id, 'source_link', sanitize_text_field( $meta_value ) );
}
}
//hide shortcode in frontend
add_shortcode('source_link', 'timd_source_link');
function timd_source_link( $atts ){
return '';
}
Arnav Joy answers:
Hello George ,
what code are you using for post creation ?
George Sprouse comments:
i am using this to create a post
https://ifttt.com/channels/wordpress/actions/31-create-a-post
I can add any div or shortcode in post.
Expert answers:
Questions:
1. The blog url added in IFTTT, is it a wordpress.com or self hosted website ?
2. Do you have access to the codebase/FTP etc for the WordPress website ?
George Sprouse comments:
Hello,
The blog url is a self hosted wordpress blog. Yes i have full access to the wordpress site.
George Sprouse comments:
Hello,
The blog url is a self hosted wordpress blog. Yes i have full access to the wordpress site.