function awepop_show_views($singular = "view", $plural = "views", $before = "This post has: ")
{
global $post;
$current_views = awepop_get_view_count();
$views_text = $before . $current_views . " ";
if ($current_views == 1) {
$views_text .= $singular;
}
else {
$views_text .= $plural;
}
return $views_text;
}
function awepop_append_to_meta($meta){
return $meta[] = awepop_show_views();
}
add_filter( 'the_meta_key', 'awepop_append_to_meta' );
I am trying to embed post views into post meta. I search alot but couldn't find the appropriate filter for post meta. please advice how can i embed my view count into post meta
Try hooking to init and using update_post_meta. For example, this is how you could increment your post views:
function my_update_postmeta() {
global $post;
update_post_meta( $post->ID, my_meta_key, $my_meta_value + 1 );
}
add_action( 'init', 'my_update_postmeta' );
Note: You would put my_update_postmeta() in your page or post template.
Ref: http://codex.wordpress.org/Function_Reference/update_post_meta
Related
I would like to use an ACF field to inject Schema merkup to a few specific pages on my WordPress website. Some of them are custom taxonomies or custom post types.
After a two hour research on the topic, I am still stuck.
I have created a text area field called schema_code and entered the desired Schema markup for some of my sub pages.
I currently use this code in my functions.php which does not do anything:
function acf_header_script() {
$schema = get_field('schema_code');
echo '<script type=application/ld+json>' . json_encode($schema_code) . '</script>';
}
add_action ( 'wp_head', 'acf_header_script' );
What am I missing here? Thanks a lot!
The second parameter of the ACF get_field() is required in this case, since you're not in the loop. It is either the post->ID or it's the taxonomy ID where it's {taxonomy_name}_{taxonomy_id} https://www.advancedcustomfields.com/resources/get_field/
Since you want to do this on pages and archives, etc... You need to first determine if it's a single page or an archive, etc.
function acf_header_script() {
// is it a single post?
if ( ! is_single() ) {
// no? get the queried object.
$object = get_queried_object();
if ( is_a( $object, 'WP_POST' ) ) {
$param = $object->ID;
} else {
$param = $object->taxonomy . '_' . $object->term_id;
}
} else {
// yes it's a single.
global $post;
$param = $post->ID;
}
$schema = get_field( 'schema_code', $param );
// if $schema is not empty.
if ( $schema ) {
echo '<script type=application/ld+json>' . json_encode( $schema ) . '</script>';
}
}
add_action( 'wp_head', 'acf_header_script' );
I have a custom post type called "professional".
After a Professional is saved (new or update) I need to override the value of a meta field called custom_permalink.
If I use this:
function afterPostUpdated($meta_id, $post_id, $meta_key='', $meta_value=''){
if(get_post_type($post_id) == 'professional' && $meta_key=='custom_permalink') {
if($_GET['message']==1) {
die($meta_value);
}
}
}
add_action('updated_post_meta', 'afterPostUpdated', 10, 4);
The die() only occurs when the field is changed. I need this to work everytime the post is saved.
OK, I found my aswer:
function afterPostUpdated($meta_id, $post_id, $meta_key='', $meta_value=''){
if(get_post_type($post_id) == 'tab_especialidad') {
if($_GET['message']==1) {
$terms = get_the_terms($post_id, 'especialidades');
$esp_slug = $terms[0]->slug;
$post_slug = sanitize_title(get_the_title($post_id));
$custom_permalink = 'especialidades/'.$esp_slug.'/'.$post_slug.'/';
//die($custom_permalink);
update_post_meta($post_id, 'custom_permalink', $custom_permalink);
}
}
}
add_action('updated_post_meta', 'afterPostUpdated', 10, 4);
I'm learning PHP and WordPress development, so I assumed that maybe here I'll find the answer or a tip.
I've restriced the_content based on user role. After the end of the_content I'd like to display button whitch is unique to the specific post. So here is the code which display that:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$aftercontent = 'I Want To Add Code Here';
$fullcontent = $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
And I'd like to insert code below into the underlined place above:
<?php
$post = $wp_query->post;
$group_id = get_field( 'link_number' , $post->ID );
if( $group_id ) {
echo do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
?>
How can I do that?
It's probably better to create a custom shortcode for this. If you change how the_content works, it will be global, everywhere.
Note:
This code is completely untested and put together after 5 min of Googling, so if somehting is wrong, feel free to comment and I'll amend it. It should be fairly close and is mostly for explaining the concept instead of a pure copy/paste solution
Register a new shortcode:
add_shortcode('my_awesome_content', 'my_awesome_content_func');
Create the callback function:
Here we've added $atts which will contain our attribute (the post id):
function my_awesome_content_func($atts = [])
{
$postId = $atts['postid'] ?? null;
if ($postId === null) {
// We got no id so let's bail
return null;
}
$post = get_post($postId);
if (!$post) {
// We didn't find any post with the id so again, let's bail
return null;
}
$group_id = get_field( 'link_number' , $post->ID );
$content = $post->content;
if( $group_id ) {
$content .= do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
}
return $content;
}
Usage:
Now you should be able to call it like this:
echo do_shortcode('[my_awesome_content postid="' . $post->ID . '"]');
You can just embed you code below in the above filter with some modifications:
function displaycontent($content) {
if (is_singular('custom_post_type')) {
$post_id = get_queried_object_id();
$group_id = get_field('link_number', $post_id);
$aftercontent = '';
if ($group_id)
$aftercontent = do_shortcode('[checkout_button class="button" level="'.$group_id. '" text="Order"]');
$fullcontent = $content.$aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
OK, thanks everyone, I Got it! Here is the code if anyone would have same problem:
function displaycontent($content) {
if(is_singular( 'custom_post_type' )) {
$group_id = get_field('link_number');
$aftercontent = do_shortcode( '[checkout_button class="button" level="' . $group_id . '" text="Order"]' );
$fullcontent = $beforecontent . $content . $aftercontent;
} else {
$fullcontent = $content;
}
return $fullcontent;
}
add_filter('the_content', 'displaycontent');
I have a shortcode [Mark] which only used as a marker for texts. And another shortcode [MarkExt] for extracting content of Marker based on the post id.
[Mark label="mrk"]Some text here[/Mark]
What I need to do is that when I invoke
[MarkExt label="mrk" id=10]
is to get the text that is enclosed by [Mark] of label mrk specified in the post id.
How can I get the [Mark ] content?
EDIT: Apologies on incomplete post . What I've done so far was
For [Mark]
function mark_shortcode($atts,$content = null){
return $content;
}
add_shortcode( 'Mark', 'mark_shortcode' );
And for [MarkExt]
function extract_shortcode($atts,$content = null){
$label = $atts['label'];
$pid = $atts['id'];
}
add_shortcode( 'MarkExt', 'extract_shortcode' );
Add following code to functions.php or plugin file.
function mark_caption_shortcode( $atts, $content = null ) {
//$content is Some text here
return $content;
}
global $mark_content;
$mark_content = add_shortcode( 'mark', 'mark_caption_shortcode' );
function extract_shortcode($atts,$content = null){
global $mark_content;
//here you will get content
$label = $atts['label'];
$pid = $atts['id'];
}
add_shortcode( 'MarkExt', 'extract_shortcode' );
I have created a custom post type in WordPress but wondering if its possible to specify an upload directory specific to the custom post type and when doing and inserting of media that the media library only show the media uploaded into the specific directory for the custom post type.
save images in and media library only shows media in:
/wp-content/uplodas/custom_post_type/
UPDATE
I have the following code which will filter the content i'm looking for HOWEVER i only want to run the filter if im on the CPT of client_gallery. My if statment doesnt seem to work?
function wpse48677_get_cpt()
{
if ($_GET['post_type'] == "") {
$posts = $_GET['post'];
$postType = get_post_type($posts);
} else {
$postType = $_GET['post_type'];
}
return $postType;
}
function my_posts_where($where)
{
global $wpdb;
$post_id = false;
if (isset($_POST['post_id'])) {
$post_id = $_POST['post_id'];
$post = get_post($post_id);
if ($post) {
$where .= $wpdb->prepare(" AND my_post_parent.post_type = %s ", $post->post_type);
}
}
return $where;
}
function my_posts_join($join)
{
global $wpdb;
$join .= " LEFT JOIN {$wpdb->posts} as my_post_parent ON ({$wpdb->posts}.post_parent = 'client_gallery') ";
return $join;
}
function my_bind_media_uploader_special_filters($query)
{
add_filter('posts_where', 'my_posts_where');
add_filter('posts_join', 'my_posts_join');
return $query;
}
print wpse48677_get_cpt();
if(wpse48677_get_cpt() == 'client_gallery') {
// THIS IF STATEMENT DOESNT SEEM TO WORK? I KNOW $doJoin == client_gallery
// AS I CAN SEE IT IF I PRINT IT OUT (see above)
// IF I RUN THE ADD FILTER WITHOUT THE IF I GET THE RESULTS I"M LOOKING FOR?????
add_filter('ajax_query_attachments_args', 'my_bind_media_uploader_special_filters');
}
you will make an upload dir for your custom post type if you add this code wherever your custom post type declaration happens. this will also use this folder everytime your upload happens from a post where the post type is yours.
function super_upload_dir( $args ) {
if(!isset($_REQUEST['post_id']) || get_post_type( $id ) != $YOUR_POST_TYPE)
return $args;
$id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : '' );
if( $id ) {
$newdir = '/' . $DIRECTORY_NAME;
$args['path'] = str_replace( $args['subdir'], '', $args['path'] );
$args['url'] = str_replace( $args['subdir'], '', $args['url'] );
$args['subdir'] = $newdir;
$args['path'] .= $newdir;
$args['url'] .= $newdir;
}
return $args;
}
add_filter( 'upload_dir', 'super_upload_dir' );
for the librairy filter, ive used plugins in the past that had a way to filter media items based on a shortcode, but i wouldnt know off the bat what to do to get it filtered based on current post type in the backend. sorry