hook save_post is not called with hook post_edit_form_tag - wordpress

I'm developping a plugin to create custom post type.
This custom post type has custom attachment fields (meta box).
In tese custom attachment fields, there is an upload field.
I have to use hook post_edit_form_tag to add enctype to the form.
add_action('post_edit_form_tag', 'updateEditForm');
function updateEditForm() {
printf(' enctype="multipart/form-data" encoding="multipart/form-data" ');
}
But with this hook, the system no longer goes into the function savePost :
add_action('save_post', 'savePost', 10, 2 );
// or add_action('save_post_vwvideo', 'savePost', 10, 2 );
function savePost($post_id, $post)
{
print_r($_FILES);
die('here');
}
If I desactivate the hook post_edit_form_tag, the system call savePost but the $_FILES is empty...
I create the custom post (vmvideo) with register_post_type and add meta box with register_meta_box_cb.
is there is a code to force the call of savePost ?
thanks
Phil

Related

Display a custom field under WooCommerce single product image

I am trying to display a custom field on Woocommerce single product page just under the product image.
Does anyone know the hook for this custom field?
You can achieve the above by adding in follows hook -
function add_custom_field_just_under_image(){
global $product;
echo "Your custom code goes here";
}
add_action( 'woocommerce_product_thumbnails', 'add_custom_field_just_under_image', 10 );
Codes goes to active theme's functions.php

How do you make a Wordpress plugin have "per post" options?

I'm building a Wordpress blog that requires a custom slideshow system with an admin panel on each post admin page (sort of like how Yoast's SEO plugin has options on each page).
How would I make the admin options appear on the post admin page?
Thanks,
Charlie
You are not providing much detail about your project but you probably want to make some meta_boxes and save the data to as custom fields.
Here is a truncated example culled from something I put together for a question at wordpress.stackexchange. The details of some of the functions are not important for your question here but it illustrates the general 'how'. Follow the link for working but sincerely beta code.
// author checkboxes
add_action( 'add_meta_boxes', 'assisting_editor' );
function assisting_editor() {
add_meta_box(
'assisting_editor', // id, used as the html id att
__( 'Editorial Tasks' ), // meta box title
'editor_tasks', // callback function, spits out the content
'post', // post type or page. This adds to posts only
'side', // context, where on the screen
'low' // priority, where should this go in the context
);
}
// this is the callback that creates the meta box content
function editor_tasks( $post ) {
// function details skipped; follow the link
}
// to save the data
add_action( 'save_post', 'save_metadata');
// callback for the save hook ^^
function save_metadata($postid) {
// function details skipped; follow the link
}

Modify custom field after save_post

In my custom post type, once the user saves the post, is there a way to check the value of one of the fields and update it? The value I will insert will depend on the post's ID so save_post needs to be used in case it's a new post.
Yes you can have all of your data from $_POST or global $post after you save or update the post using save_post hook as you mentioned in your question
add_action( 'save_post', 'afterSavePost' );
function afterSavePost($pid)
{
$postId=$pid;
// or
global $post;
$postId=$post->ID;
$postTitle=$post->post_title;
// or
$postId=$_POST['ID'];
$postTitle=$_POST['post_title'];
}
You mentioned custom field and in that case you can use
$yourCustomField=get_post_meta($postId, 'your_custom_field',true); // get a custom field
and
$yourCustomField="New value";
update_post_meta($postId, 'your_custom_field', $yourCustomField); // update a custom field

display custom fields automatically when a custom post type is displayed

I am trying to automatically display all the custom fields of a custom post type alongside it's title and content.(Not in admin but on my actual site)
I need to be able to do this with an action hook or filter, rather than creating a template.
After scouring the web I was able to find the 'publish_{custom_post_type_name}' hook:
function my_cool_hook() {
echo get_post_meta($post->ID, 'my-custom-field-name', true);
}
add_action( 'publish_past_symposia', 'my_cool_hook' );
but it doesn't seem to do anything when I view my published custom post type on my site. Any ideas?
add_action( 'publish_past_symposia', 'my_cool_hook' );
This hook triggered only if PUBLISH post type.
YOu need to trigger the hook on web part - so...
add_filter('the_content', 'my_cool_hook');
function my_cool_hook($content){
return $content.get_post_meta(get_the_id(), 'my-custom-field-name', true);
}
now the content body filtred and your string from custom fields added.

Adding a function to wordpress theme to bypass/replace premalinks

Looking at Cssremix.com
when you hover over a item you can see the "Views" function I'm assuming they are using a plugin that links with the post views but when clicked the item redirects not to the post but to a website/site used
This plugin gives you the post views. To filter a link you can hook into certain filters, such as post_link or the_permalink.
Here are some docs:
Plugin API usage
Filter reference
The usage would be something like this:
add_action('post_link', 'do_custom_link');
function do_custom_link($url, $post) {
$external_url = get_post_meta($post->ID, 'external_url', true);
if($external_url) {
return $external_url;
}
return $url;
}
This would get the external url from a meta field stored with the post, called external_url. You would define that meta field using the custom fields UI when you create the post through the admin pages.

Resources