Plugin for custom field? - wordpress

This plugin https://wordpress.org/plugins/no-external-links/
I’m using a custom field.
<?php $values = get_post_custom_values("imgurl"); echo $values[0]; ?>
The plugin does not mask this my custom field. How do I mask this link?
What code should I add to the function file? thanks
// You will have to add just a line in a theme code where you output custom field data.
// To add same preprocessing for data as for comment text, use
$metadata = apply_filters(‘comment_text’, $metadata);
// For example, if you use some kind of metadata, it should look like this:
$metadata = get_post_meta($id, ‘MetaTagName’, true); // get data from wordpress database
$metadata = apply_filters(‘comment_text’, $metadata); // add this line of code for preprocessing field value
echo $metadata; //output preprocessed field value
// Use “the_content” instead of “comment_text” if you want to use the same masking policy as for post text. https://www.ozgurbilgi.net

Related

Wordpress: Can I use get_post_meta in my Plugin?

So I'm trying to reference custom field values in a plugin I'm building. All I need to do at this stage is grab the values and store them in variables. This is my code to get the custom field value of pageName:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$pageName = get_post_meta($postid, 'pageName', true);
wp_reset_query()
?>
So when I try to echo that out, I get nothing. I notice that my plugin runs before the head or anything else, so it's the first code in the source. My hunch is that this is due to timing and the value just isn't there yet. Is there a way to make my plugin, or this chunk of code, wait until the custom field values are there before trying to grab them?
I'm trying to avoid doing anything in the theme files so this can be a stand alone plugin that I can share.
yes, you can get the value of any post meta of the custom post type.
Just make sure that you are receiving the correct post_id in the $postid variable.
If you get the correct id of the post type you can get any meta field
Example:
global $post;
if ($post->ID) {
$media_id_meta = get_post_meta($post->ID, 'media_id', true);
}
Found the solution! I wrapped the whole thing in a function to put it in the footer, which made sure that everything it needed was there.
//----This function is wrapped around the code for my plugin
function dataLayerInject() {
*ALL MY CODE*
}
//----This drops my code into the footer
add_action('wp_footer', 'dataLayerInject');

Add WordPress filter to ACF WYSIWYG field

I am currently using the following filter to replace any instance of the string 'magic_click_link' within WordPress' the_content.
function click_link ($b) {
global $post;
$this_post_id = $post->ID;
$op_name = get_field('operator_name');
$namenospace = make_no_space("$op_name");
$tracking_link = '/go/'.$this_post_id.'/';
$click_link = '<a class="claimCTA" id="operator-step1-'.$namenospace.'" href="'.$tracking_link.'" target="_blank" rel="nofollow">Click here to go to '.$op_name.'!</a>';
$b = str_ireplace('magic_click_link',$click_link,$b);
return $b;
}
add_filter( 'the_content', 'click_link');
I have just updated this field to use an ACF WYSIWYG field, but this has caused the filter to stop working.
I believe I need to target something other than the_content inside the filter but I'm not sure what is needed...
The filter the_content automatically runs on any content output using the function the_content(), so by default, this means it only applies to standard WordPress post and page content.
To apply these filters to your custom field content as well, you need to manually call apply_filters() when you output the field's content.
For example, say your custom field is called my_extra_content. With Advanced Custom Fields, you can call:
echo apply_filters("the_content", get_field("my_extra_content"));
This will output the content of your field, while applying all the usual filters against it. Because your click_link() function is hooked to the_content already, it will therefore process your custom field content as you are desiring.

How to output cmb2 wysiwyg by using timber

I am using cmb2 to create the custom post type, and I have a custom post type called legacy_cycle, which contains several wysiwyg fileds.
Than I am using Timber as theme tool to display the input. However, the wysiwyg field could not work properly. It is fine if it only contains text or image, but it only output the following information when I insert a youtube video in the wysiwyg editor under the custom post type, but the output from the native wordpress editor is fine.
" [embed]https://www.youtube.com/watch?v=MS91knuzoOA[/embed]"
I tried to use post.get_filed('my_wysiwig'), but it did not work.
I am wondering how I could output the field correctly? much appreciated!
So, I figured it out through this post..Applying oembed filters to WYSIWYG field
My solution by using timber/twig is to get the data in the single.php, and here is the code:
$post_meta = get_post_meta(get_the_ID(),'my_wysiswg', true);
$post_meta = $wp_embed->autoembed( $post_meta );
$post_meta = $wp_embed->run_shortcode( $post_meta );
$post_meta = do_shortcode( $post_meta );
$post_meta = wpautop( $post_meta );
$post->my_wysiswyg = $post_meta;
Then I can print the video in the single-custom-post-type.twig by using {{post.my_wysiswyg}}
It looks like you just need to process shortcodes in that field. This should convert those into actual YouTube videos:
{{ post.get_field('my_wysiswyg') | shortcodes }}
I use CMB2 extensively with Timber and my usual approach is to extend TimberPost and add in methods for getting meta data. For a wysiwyg field, something like this:
class CustomPost extends TimberPost {
public function my_wysiswg(){
$metadata = get_post_meta($this->ID, 'my_wysiswg', true);
if ($metadata){
return apply_filters('the_content', $metadata);
}
}
}
You can specify which class Timber will use with the second parameter of Timber::get_posts

Options panel global variable?

I am creating a WordPress options panel based on
http://en.bainternet.info/2012/my-options-panel
In the article it states to call the stored options to use
//get the data to an array
$data = get_option('demo_options');
//access each field by its id
echo $data['text_field_id'];
Which works, the only problem I have is that if I want to call some information for say the header.php and the footer.php or any other page I have to include the line
$data = get_option('demo_options');
at the top of every page otherwise I can not call the data, which seems repetitive.
I have tried to create a global variable in the functions.php file like;
global $data
$data = get_option('demo_options');
but that doesn't work.
Does anyone know how I can resolve this so I don't need to add the line to the top of every page?
Thanks
In functions.php:
$data = get_option('demo_options');
In any other theme template file (header.php, single.php, page.php):
global $data;
var_dump( $data );
To have a variable defined in header.php and not having to use global $var in other template files, the following has to be done:
define the variable in header.php
instead of get_header(); in other theme template files, use include 'header.php';
this way, you can reference the variable directly without having to declare the global

Wordpress: add custom page attribute

I have the NextGEN Gallery plugin on my wordpress site. Normally I would add this short code in my page content to display gallery items:
[ nggallery id=5 template=custom ]
Now I'd like to replace this by adding custom fields in the Page Attributes setting when you are adding/editing a page. The custom fields would be "Gallery ID" and "Template name".
I'm of course using a custom page template. How can I retrieve the page attributes into this page template?
Thanks in advance!
Yes, what Stratboy said. Documentation here. This is the setup that should work for you:
<?php
$gallery_ID = get_post_meta($post->ID, 'Gallery ID', true);
$template_name = get_post_meta($post->ID, 'Template Name', true);
if ($gallery_ID && $template_name){
//echo '$gallery_ID: '.$gallery_ID.'; $template_name: '.$template_name.';';
echo do_shortcode('[nggallery id="'.$gallery_ID.'" template="'.$template_name.'"]');
}
?>
So, first:
have you already implemented the custom page template?
have you already implemented the custom fields?
Anyway, generally, in a template you get your custom fields values using the get_post_meta function in the Loop, like this:
//the last param tell if you want the value returned as a string (true) or an array (false)
get_post_meta($post->ID,'field name',true);
You can echo the returned value or use it for other tasks.
Let me know.

Resources