WPML how to add a featured image without duplicate to a translated post (wordpress)? - wordpress

I translated a post using WPML.
The problem is that it doesn't add a featured image to the translated post.
It takes up too many size to duplicate images using a WPML media transltion,
so I'd like to add a featured image without duplicate to a tranlsated post (wordpress).
Would you please let me know how to solve this problem?
Thank you.

In case there is someone who needs this.
I solved the problem using the foolowing code.
add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
function my_duplicate_on_publish( $post_id ) {
global $post;
// don't save for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// dont save for revisions
if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
return $post_id;
}
if ($post->post_type=='your-post-type') {
//we need this to avoid recursion see add_action at the end
remove_action( 'wp_insert_post', 'my_duplicate_on_publish' );
// make duplicates if the post being saved
// #1. itself is not a duplicate of another or
// #2. does not already have translations
$is_translated = apply_filters( 'wpml_element_has_translations', '', $post_id, $post->post_type );
if ( !$is_translated ) {
do_action( 'wpml_admin_make_post_duplicates', $post_id );
}
//must hook again - see remove_action further up
add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
}
}

Related

Wordpress redirect if URL parameter is empty for each post

So there are people who access my website directly (not thru the tracking link I got with Voluum), thus they are not able to click the links and I can't see them as a part of my stats.
How can I redirect users who don't have a /?voluumdata=BASE64... URL parameter to a tracked URL, and to have a different redirect for each blog post?
I was testing and looking for a plugin / .htaccess trick for hours but nothing seemed to help.
Edit: I found a solution that I was certain is going to work but for some reason it didn't:
[insert_php]
if(empty($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Also tried:
[insert_php]
if(!isset($_GET['voluumdata']))
{
header('Location: REDIRECT_URL');
exit;
}
[/insert_php]
Both just break the page loading proccess.
Unfortunately, I cannot understand what is the purpose of the code you have entered in your question. I mean that is not clear the reason you use the tags [insert_php].
A solution to your problem it can be the following.
function redirect_direct_access( ) {
// You may use the code:
//
// global $wp_query
//
// in order to determine in which pages you should run your
// redirection code. If you only check for the token existence
// then you will be faced with redirection loop. I don't explain in
// depth how to use the $wp_query as it is not part of your question
// but you always have the opportunity to check what is the contents
// of this variable using the code:
//
// echo "<pre>";
// print_r( $wp_query );
// echo "</pre>";
//
// This way you will be able to build your specific if statement
// for the page you like to test.
if (
! isset( $_GET[ 'voluumdata' ] ) ||
empty( $_GET[ 'voluumdata' ] )
) {
wp_redirect( home_url( '/page/to/redirect/' ) );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );
You can find more information in the WordPress documentation related to the template_redirect hook.
Just in case anyone would face the same issue, #Merianos Nikos has given a half-answer and I mastered it into this:
function redirect_direct_access( ) {
$post_id = get_the_ID();
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
if (
$post_id == POST_ID &&
!isset( $_GET[ 'voluumdata' ] )
) {
wp_redirect( 'REDIRECT_URL' );
exit();
}
}
add_action( 'template_redirect', 'redirect_direct_access' );

Trigger action when new post is insert in wordpress

My question is,
Is there any filter or action that trigger when new post is insert in database..?
The reason behind it is I want to add key in post meta when new post is insert from admin side.
I got Action called "save_post" but after refer link .This action trigger in created and update post.
but I only want to add meta key when post is created not at update time
You can use wp_insert_post so you will get post_id as soon as post inserted and you can then use that to add meta_key.
If you are not using wp_insert_post and want to use action then you can simply put below code :
if ( wp_is_post_revision( $post_id ) )
return;
which means that if you are updating the post, then it will return back from function.
EDITED
Method-1 to achieve it.
You can simply check with the get_post method that post is there or not.something like below:
add_action('save_post', 'check_for_post_in_database');
function check_for_post_in_database($post_id) {
//check if the post is in the database or not with get_post( $post_id ) == null
if( get_post( $post_id ) == null ) {
//your code to add meta
}
}
//You can do same thing with publish_post
Method-2 to achieve it.
add_action('publish_post', 'check_for_meta_in_database');
function check_for_meta_in_database($post_id) {
global $wpdb;
$your_meta = get_post_meta($post_id, 'meta_key', true);
if( empty( $your_meta ) && ! wp_is_post_revision( $post_id ) ) {
update_post_meta($post_id, 'meta_key', 'meta_value');
}
}
But as you said there are many meta there, this method will be bit long.
Method-3 to achieve it.
You can do as rnevius suggested which is the one even I would opt. Its like :
add_action( 'transition_post_status', 'check_transition_and_then_add_meta', 10, 3 );
function check_transition_and_then_add_meta( $new_status, $old_status, $post ) {
if ( ( 'draft' === $old_status || 'auto-draft' === $old_status ) && $new_status === 'publish' ) {
add_post_meta($post->ID, 'your_meta_key', 'your_meta_value');
}
}
or else you can do it with draft_to_publish like:
//as rnevius suggested {$old_status}_to_{$new_status}
add_action( 'draft_to_publish', 'add_meta_when_status_change' );
function add_meta_when_status_change() {
add_post_meta($post->ID, 'your_meta_key', 'your_meta_value');
}
You can refer codex for more information about post transition.
You're looking for draft_to_publish.
An {old_status}_to_{new_status} action will execute when a post transitions from {old_status} to {new_status}. The action is accompanied by the $post object.

WooCommerce inquiry if no price available

I'm helping a good friend setting up a WooCommerce shop. Since the shop is going to be bigger and the products are pretty variable and customizable we are not able to provide/configure all prizes from the beginning.
However we would like all products to be in the shop and ad an inquiry lead form in case no price is available.
Since I never programmed with WooCommerce I was wondering that is the right hook to implement such an functionality?
Had the exact same issue and couldn't find a plugin or a solution anywhere so I figured a workaround myself:
You need to edit file
/wp-content/themes/your-theme-name/woocommerce/single-product/add-to-cart/simple.php
(if it's not there just copy it from woocommerce plugin
/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/simple.php)
and on line 14 where it says
if ( ! $product->is_purchasable() ) return;
you need to comment it out and write something like
if ( ! $product->is_purchasable() ) {
// put your code here
return;
}
and in the //put your code here line you can enter for example a shortcode for a form or a more complicated solution would be to put code for a button that when clicked will open up a popup form.
Still working on that ;)
Maybe too late, but I have had same issue currently.
Here is there code, Woocommerce uses to check if a product can be purchased:
https://github.com/woocommerce/woocommerce/blob/master/includes/abstracts/abstract-wc-product.php#L1404
Notice about: && '' !== $this->get_price()
/**
* Returns false if the product cannot be bought.
*
* #return bool
*/
public function is_purchasable() {
return apply_filters( 'woocommerce_is_purchasable', $this->exists() && ( 'publish' === $this->get_status() || current_user_can( 'edit_post', $this->get_id() ) ) && '' !== $this->get_price(), $this );
}
So you need to write a filter like this to override default:
add_filter( 'woocommerce_is_purchasable', function ( $is_purchasable, $product ) {
return $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) );
}, 10, 2 );
Try the following code snippet. You just have to put it in your functions.php. You don't need a plugin or to overwrite WooCommerce files in your child theme.
// Inquiry link if no price available
function add_inquiry_link_instead_price( $price, $product ) {
if ( '' === $product->get_price() || 0 == $product->get_price() ) :
return ''.__( 'Jetzt anfragen' ).'';
endif;
}
add_filter( 'woocommerce_get_price_html', 'add_inquiry_link_instead_price', 100, 2 );

Wordpress - Get Attachment url when publish post

I have a problem. I want to get the featured image url when a post was published.
It works when I update a post, but not when it was published for the first time, because the meta data seems not to be stored in the database at this moment. Even when I use 'wp_insert_post' instead of 'save_post' it does not work.
In my functions.php i check for new/updated posts with:
add_action( 'save_post', 'my_function' );
When a post was updated I read the featured image url by using:
$image_url = get_post_meta( get_post_meta( $post_id, "_thumbnail_id", true ), "_wp_attached_file", true );
Can you help me?
Well, if you want to take attachments from the post you are publishing, save_post is a no go.
Try publish_post
At the moment when publish_post is fired, the post and its attachments already exists in the database and can be accessed.
save_post action hook runs after the data is saved to the database ( wordpress codex ), so this should do it, it works on post publish and post update. A couple of useful links is commented within the code.
// http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
add_action( 'save_post', function ( $post_id ) {
if ( wp_is_post_revision( $post_id ) ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// save_post action is executed on all post actions like publish, trash, auto-draft, etc.
// http://codex.wordpress.org/Post_Status_Transitions
if ( get_post_status( $post_id ) == 'publish' ) {
// http://codex.wordpress.org/Function_Reference/get_post_thumbnail_id
$thumb_id = get_post_thumbnail_id( $post_id );
// http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
$thumb_url = wp_get_attachment_image_src( $thumb_id, 'full' );
$thumb_url = $thumb_url ? $thumb_url[0] : false;
}
});

Wordpress admin search post error - invalid post type

I have a website with some custom post type created with Ultimate CMS plugin.
In admin area, when I make a new search, the result is ok, but after that, when I'm try to make a second search, It give me an error "Invalid post type".
I also realize that the url it's not ok:
In the first search, the url is something like this:
http://www.site.com/wp-admin/edit.php?s=SearchTerm1&post_status=all&post_type=post&action=-1&m=0&cat=0&paged=1&mode=list&action2=-1
In the second search, the url is something like:
http://www.site.com/wp-admin/edit.php?s=SearchTerm2&post_status=all&post_type=Array&_wpnonce=4d88f268e4&_wp_http_referer=%2Fwp-admin%2Fedit.php%3Fs%3DSearchTerm1%26post_status%3Dall%26post_type%3Dpost%26action%3D-1%26m%3D0%26cat%3D0%26paged%3D1%26mode%3Dlist%26action2%3D-1&action=-1&m=0&cat=0&paged=1&mode=list&action2=-1
And the error message: "Invalid post type".
I deactived all of my plugins, I upgraded wordpress to the latest version 3.5.1, I reset permalinks to default, but this error still remain.
Any help would be much appreciated!
Thank you
I also came across this issue and found that it was a result of one of my functions in my functions.php file that was modifying the global wordpress query parameters.
It sounds like you edited the global $query object. If you used a hook such as 'pre_get_posts' and manipulated the $query object and you do not exclude the admin area, then any changes you make to the query parameters will also apply to the admin panel and it will display that error when trying to add in parameters that don't fit your search.
For example:
Let's say you have a search feature on your site, and when the user enters a search and goes to the search results page, you only want to display posts of a custom post type called $searchable_posts, then you would add a hook to your functions.php file like this:
function searchfilter($query) {
if ($query->is_search && $query->is_main_query() ) {
$query->set('post_type', $searchable_posts);
}
return $query;
}
add_filter('pre_get_posts', 'searchfilter');
This will make it so that any global default $query will only search for results that have a matching post type of $searchable_posts. However, the way it is written above means this will also apply to any global $query in the admin panel also.
The way around this is to structure your query like this:
function searchfilter($query) {
if ($query->is_search && $query->is_main_query() && !is_admin() ) {
$query->set('post_type', $searchable_posts);
}
return $query;
}
add_filter('pre_get_posts', 'searchfilter');
Adding in that !is_admin() means that your function will exclude anything in the backend administration panel (see is_admin ).
Alternatively, a safer way if you can, instead of using the global default $query is to create your own new WP_Query and searching using that instead - the codex has good examples of how to set that up.
I am also get the issue same as you and finally I got the solution.
We have to add $query->is_main_query() in IF CONDITION.
Below is the full code.
function acf_meta_value_filter() {
global $typenow;
global $wp_query;
if ( $typenow == 'your_custom_post_type_name' ) { // Your custom post type meta_key_name
$plugins = array( 'value1', 'value2', 'value3' ); // Options for the filter select field
$current_plugin = '';
if( isset( $_GET['meta_key_name'] ) ) {
$current_plugin = $_GET['meta_key_name']; // Check if option has been selected
} ?>
<select name="meta_key_name" id="meta_key_name">
<option value="all" <?php selected( 'all', $current_plugin ); ?>><?php _e( 'All', '' ); ?></option>
<?php foreach( $plugins as $key=>$value ) { ?>
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $current_plugin ); ?>><?php echo esc_attr( $value ); ?></option>
<?php } ?>
</select>
<?php }
}
add_action( 'restrict_manage_posts', 'acf_meta_value_filter' );
function acf_meta_value_filter_by_slug( $query ) {
global $pagenow;
// Get the post type
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
if ( is_admin() && $pagenow=='edit.php' && $post_type == 'your_custom_post_type_name' && isset( $_GET['meta_key_name'] ) && $_GET['meta_key_name'] !='all' && $query->is_main_query()) {
$query->query_vars['meta_key'] = 'meta_key_name';
$query->query_vars['meta_value'] = $_GET['meta_key_name'];
$query->query_vars['meta_compare'] = '=';
}
}
add_filter( 'parse_query', 'acf_meta_value_filter_by_slug' );
I had a similar problem with a site a took over from other dev.
function anty_search_form( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post', 'product') );
}
return $query;
}
add_filter( 'pre_get_posts', 'anty_search_form' );
Someone forgot to exclude this function from wp-admin page. So changing to this helped Invalid post type.
function anty_search_form( $query ) {
if (!is_admin()){
if ( $query->is_search ) {
$query->set( 'post_type', array('post', 'product') );
}
return $query;
}
}
add_filter( 'pre_get_posts', 'anty_search_form' );
At wp-admin when submitted the second search for a custom post type, I was getting the same error, "Invalid post type." The URL seemed long and incorrect. Anyway, the following code worked perfectly:
add_filter( 'pre_get_posts', 'tgm_io_cpt_search' );
function tgm_io_cpt_search( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', 'your_custom_post_type' );
}
return $query;
}

Resources