wp auto insert tag in posts - wordpress

when I edit a post in wp sometimes I don't assign a tag , is it a good Idea to auto insert a tag like the category name or my site name instead of no tag at all?
and if its better to input a tag then I need help writing a function that check if the post has no tag then it will insert lets say my sitename.
all what I have is
wp_set_post_tags( 42, 'mepanorama', true );
but this will work only on specific post and does not check if there is a tag already.

This will return the number of tags:
count(wp_get_post_terms($post_id, 'post_tag', array("fields" => "names")));
I made a plugin that adds tags to posts automatically, it is called Automatic Post Tagger. Check it out, it might be useful for you. :)

Related

WordPress Gutenberg, update post content programmatically

I have been starting to testing out Gutenberg editor with both ACF and custom blocks. And I have been looking around to solve my problem but I couldn't find anything about this topic (Maybe my google skills is not good enought)
But my case is this:
I have a custom post type where I want to set a template so they can't move around the blocks and add other blocks and so on. And on this post type around 70% is created by code. Because it is fetching all the information from an API.
Test 1: I have created an ACF block with all the fields I need, and it is working as it should when I create a new post from WP admin. But when I run the update_field function it is saving it to post_meta table as it did before. So my question here is how do I update a field so it saves it to post_content and not to post_meta table.
Test 2: I created custom blocks for all of the fields (convert each ACF field to and block) and set up the template to use these blocks only.
But here I have no idea how update update post_content with PHP or Javascript.
I hope you can help me out with this :) If anything is unclear tell, and I will try to explain it
ACF has an ability to pre-init fields before post will be visible to user on post creation page. You can try to use this function to set desired content to fields.
You can read about this here:
https://www.advancedcustomfields.com/resources/acf-prepare_field/
As of Wordpress 5.0.0
You can use template and template_lock arguments upon registering your custom post type.
Source # https://developer.wordpress.org/reference/functions/register_post_type/#changelog
You can then set an array of specific blocks to use and you can chose to restrict users from adding new blocks or removing them.
Attribute
Description
template
(array) Array of blocks to use as the default initial state for an editor session. Each item should be an array containing block name and optional attributes.
template_lock
(string/false) Whether the block template should be locked if $template is set. If set to 'all', the user is unable to insert new blocks, move existing blocks and delete blocks. If set to 'insert', the user is able to move existing blocks but is unable to insert new blocks and delete blocks. Default false.
A short example would be something along the lines of...
<?php
$args = [
//...
'template_lock' => 'all',
'template' => [
[ 'core/paragraph' ],
[ 'core/file' ],
//...
],
//...
];
register_post_type( $post_type, $args );
?>
Currently, Gutenberg documentation is scarced, you can find a complete list of blocks & parameters # https://github.com/WordPress/gutenberg/tree/master/packages/block-library/src
Gutenberg is still in development, some features don't act as they should.

woocommerce checkout country

Hi and thanks in advance for any help,
I need to debug the woocommerce checkout page in which some fields of the form billing are not aligned correctly for some countries.
i saw that the issue is resolved if i add a after the postcode field, the function woocommerce_form_field( $key, $args, $value = null ) adds that div if $args['clear'] is not empty and it's called in a foreach for the checkout page in the checkout/form-billing.php file.
woocommerce_form_field(...) is in woocommerce/includes/wc-template-functions.php.
The $args contains the clear attribute correctly and woocommerce_form_field returns the field with div correctly (mailed myself the data from the function), when i get to the page the div is not there and fields are not aligned. Also i noticed that if i try to echo something it doesn't appear on the page source but if i mail the data from the same form-billing.php spot the data is received correctly.
How can i make the div show correctly ? What javascript could rewrite the form and where can i edit it ?
the fields disaligns if you select United States in the country select.
Any suggestion would be appreciated
P.S.: i added some javascript that adds the div on document.onload but it feels dirty and i'm not sure it works in every case.
EDIT: rephrase, clarify
To be safe when updating woocommerce in future versions you should do the following:
Best is to replace checkout.min.js with your own modified version.
Placing this in your functions.php should load your modified script
$wp_scripts->registered[ 'wc-checkout' ]->src = get_stylesheet_directory_uri() . '/js/yourmodifiedcheckout.js';
Hope this helps, let me know if you need further help.

Page Template for every custom post type

I'm just new in wordpress, I just wanted to ask if it's a bad practice if I'll create page template for every custom post types?
I mean if I have CPT [custom post type] with different content, like my first CPT has images, my 2nd cpt has image and text, my last CPT, has slider, text and description.
Because I wanted to create page template for every type of CPT's I have. Is it a bad practice? Or are there efficient and effective ways to do such things?
Your answers are highly appreciated. Thanks!
It's better not to do that, not only because it's a server load, but also because you'll get crazy if you have an issue. Instead, use conditionals in the same page. You can read about it at WP Codex: Conditional Tags : Taxonomies and then pay attention to the is_tax and has_term tags.
This way, you can use is_tag or has_term depending on your approach, like this
if( has_term( 'jazz', 'genre' ) ) {
// do something
}
You can use taxonomy to deal with such problem. Use taxonomy to filter contents according to your requirements. Use register_taxonomy() to register a taxonomy for CPT.

Create a blog post from content in another post type

The Wordpress site I'm working on has a section for "News" (which is the regular blog/posts) which will be used for any news the company has to write about. Then I have a custom post type for Promotions, which has it's own page.
I want the client to be able to add his promotion content through the custom post type, which is going on the Promotions page, however I'd like this content to also be "cross posted" into the blog/news without forcing the client to write it up twice.
Is there a way to do this? Thanks.
Just a note: The reason I have the promotions as a custom type on it's own instead of just having them do it all from the blog is because I needed custom fields that would be unnecessary for any other kind of blog post.
Two options:
1) Use the Shortcode API
And in your cross-post you'd add the shortcode [crosspost id="POST-ID"]. Where POST-ID corresponds to the numeric ID of the other post (post type). Instead of ID, the title could be used, see the function get_page_by_title.
Create your own plugin for this. Add a sample shortcode from the Codex and use the function get_post to get the contents of the cross-post.
2) Use Advanced Custom Fields plugin
With it, adding meta boxes with custom fields is a breeze. And it has a Post Object field that's basically a Cross Post functionality.
You could do it a lot more simply by adding a filter to wp_insert_data(). For example, in your theme's functions.php file add the following:
add_filter('wp_insert_post_data', 'post_to_other', 99, 2);
That filter will then run anytime you add a new post. In the function post_to_other(), you look to see what type of post is being submitted. If it's a promotion, then insert a second copy as a News item.
function post_to_other($post_id, $post){
/** check $post to see what type it is, if it's a promotion */
if($post->post_type == 'promotion'){
$second_post = array(
'post_type'=> 'post',
'post_title'=> $post->post_title,
'post_name' =>$post->post_name,
'post_content'=> $post->post_content,
'post_author'=> $post->post_author,
'post_status'=> 'publish',
'tax_input'=> array('taxonomy_name'=>array('news'))
);
wp_insert_post($second_post);
}
}
I'm running out the door so I don't have time to double check the exact code but that's the basic structure of it. The tax_input bit is optional, lets you specify a category if you want. You'll probably need to tweak it a bit but that's the basics.

Wordpress: How to pass additional Content to the blog-preview page?

For each blog-post on my wordpress-blog I'd like to have Teaxtarea where i can pass additional content for that post.
In my case that would be an unordered list which contains a quick overview of the content.
That additional content should be displayed in the preview of the post on the blog-preview-page.
My problem:
I am actually not sure on how to best add this additional content and then pass it to the preview.
Do I use wordpress' custom fields for something like this?
I'm gratefull for a push in the right direction.
Thank you,
Nils
If I understand you right, I'd take a look at "custom meta boxes" functionality - it allows you to add any type of additional input into your blog post admin area, and than display its content on front-end however you like.
There's a nice tutorial series on that topic, with example code snippets:
http://wp.tutsplus.com/series/reusable-custom-meta-boxes/
And if you'd like to display the textarea content only in preview mode, you can use appropriate conditional tag in you template file:
http://codex.wordpress.org/Conditional_Tags#A_Preview
The conditional tag is_preview returns true when a single post is viewed in Draft mode. The following will append post meta to the content when a post is being previewed:
function so16799607_preview( $content )
{
if ( ! is_preview() )
return $content;
return $content . get_post_meta( get_the_ID(), 'my_post_meta', true );
}
add_filter( 'the_content', 'so16799607_preview', 10, 1 );
You should check out Advanced Custom Fields. That's a really stable plugin that lets you create custom meta boxes in posts, pages and custom post types. That plugin does exactly what your question states. Need al little PHP to get stuff from your database, but that is as easy as:
<?php the_field(field_name);?>
And the documentation is pretty good. And if you don't like a plugin, it exports the PHP as well.
Anther tool that does the same is Pods Framework. Both powerfull extensions to any WP install in my opinion.
Hope this helps.

Resources