I'm trying to update a variable that is getting recieved by a session. Now it will save the data but it is for every post the same. It needs to save the value by post id.
A part of the code is (it's part of a Advanced Custom Field template):
<?php
function create_field( $field )
{
$value = $_SESSION['updatevalueMax'];
echo '<div id="value">' . $value . '</div>';
}
function update_value( $value, $post_id, $field )
{
return $value;
}
?>
The update_value function is activated when a post is updated. How to make this work so the $value is updated by $post_id? Thanks.
You will need to register the filter with Wordpress:
function my_plugin_update_value( $value, $post_id, $field )
{
// Do something and update $value.
return $value;
}
add_filter('acf/update_value', 'my_plugin_update_value', 10, 3);
Although in your example $value is just going to be what is sent into the function. I am not sure what you trying to update it with.
You will need to write a filter in your functions.php
follow the url to better understand.
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
Related
I am running a function within functions.php to pre-populate an acf field. I need to set a variable which will change depending on which post type. When I run get_post_type(); it does not work. I have tried many variations such as:
get_post_type($post->ID);
$id = get_queried_object_id();
$this_is_post_type = get_post_type($post->$id);
I have also used this wordpress function to no avail:
function get_post_type( $post = null ) {
$post = get_post( $post );
if ( $post ) {
return $post->post_type;
}
return false;
}
Can anybody help me get the post type and store it within a variable I can use please? I am out of ideas. If I use get_post_type within the template files it works fine, it is only at the global level - outside of the loop - that it doesn't.
Based on your comments, you are in fact inside of a filter so you should be able to access what you need. However, because you are also targeting new things, not all functions will behave as you expect, specifically get_queried_object() and similar, and this is a case where the global $post is probably the best way.
I ran this through a WordPress site that I have locally and it worked as expected. I'll leave the logic of the interior parts up to you.
add_filter(
'acf/load_field/key=field_62b1c53ac1a88',
function ($field) {
global $post;
// This should always be true because you are targeting a very specific
// key, however better safe than sorry.
if ($post instanceof \WP_Post) {
// Your logic here
if ('news' === $post->post_type) {
$field['default_value'] = 3;
$field['disabled'] = true;
}
}
// Always return something, no matter what
return $field;
}
);
I have a custom post type named 'project' and using a template for it "single-project.php"
which is working fine.
Now I want to have a some differentiation, what I need to assign the template "single-project.php" if a custom value in 'project' post type is mobile and "project-website.php" template if that custom meta value is equal to website.
means, need code to change the templates dynamically on the basis of custom meta value.
Thanks
The easiest way is to just call the "project-website.php" file from the "single-project.php" file, if the custom meta value is equal to "website".
Edit your "single-project.php" file:
<?php
$custom_field_value = get_post_meta($post_id,'your-custom-meta-key',true);
if($custom_field_value == 'website'){
echo get_template_part('single-project');
} else {
*** ENTER YOUR CURRENT CODE FOR SINGLE-PROJECT.PHP HERE ***
}
?>
#Jeremy good point, I already know this but I did it this way , Thanks,
function get_custom_post_type_template($single_template)
{
global $post;
$object = get_queried_object();
$id = $object->ID;
$post_type = $object->post_type;
$is_mobile = get_field('is_mobile_app', $id);
if ($is_mobile !== true && $post_type == 'project') {
return $single_template = dirname(__FILE__) . '/single-project-website.php';
} else {
return $single_template;
}
// return $single_template;
}```
I'm using ACF with WordPress and I have a form that I am trying to add a validation error to, but it doesn't appear to be working (I think I know why)...
My code:
function my_acf_update_value( $value, $post_id, $field ) {
// Get the users numerical user id
$user_id = str_replace('user_', '', $post_id);
$value = sanitize_text_field($value);
/** #noinspection PhpParamsInspection */
$user_data = wp_update_user(array('ID' => $user_id, 'user_email' => $value ) );
if ( is_wp_error( $user_data ) ) {
$wp_error = $user_data->get_error_message();
$input = $field['prefix'] . '[' . $field['key'] . ']';
acf_add_validation_error($input, $wp_error);
}
return $value;
}
add_filter('acf/update_value/key=field_5c121023e119f', 'my_acf_update_value', 10, 3);
As you may be able to tell I am attempting to update the users email address field (default WP one) based off the email provided by a user in an email field on a frontend ACF Form.
I then check to see if the update caused any errors and obviously if it did I want to add to the ACF validation errors, but it passers through successfully.
I assume this is because the update function runs AFTER the validation and this is why it isn't working?
Because of this I thought about doing the update in the validation function such as:
function my_acf_validate_value( $valid, $value, $field, $input ){
// bail early if value is already invalid
if( !$valid ) {
return $valid;
}
// Some code...
// Return error here?
return $valid;
}
add_filter('acf/validate_value/key=field_5c121023e119f', 'my_acf_validate_value', 10, 4);
...but I don't seem to have access to the $post_id here, nor does this seem like the best way to handle it?
Is there a way to handle this better?
You can use var_dump to see what data you have within the function - either in one of the existing function attributes, or by using get_the_ID() or global $post; to access the current Post object. Beware when trying to manipulate the Post object here, though.
What would be the correct way to add a filter to replace part of a string in an echo?
The function is this:
function woocommerce_template_loop_product_title() {
echo '<h3>' . get_the_title() . '</h3>';
}
I tried to implement this in my functions.php:
add_filter( 'woocommerce_template_loop_product_title', 'product_title_mod');
function product_title_mod($str) {
return str_replace ('h3','h2',$str);
}
But that does not affect the h3 values, what would be the best way to filter those be?
function should return value so you can use it or echo value directly:
add_filter( 'woocommerce_template_loop_product_title', 'product_title_mod');
// option 1
function product_title_mod($str) {
return str_replace("h3","h2",$str);
}
// option 2
function product_title_mod($str) {
echo str_replace("h3","h2",$str);
}
Not totally sure how wordpress filters work, but I suppose you can't achieve what you want, because woocommerce_template_loop_product_title function echoes result directly.
When we add new post in wordpress, after supplying the post title, the slug is generated automatically. I need to edit that auto generation module so that i can add some arbitrary number in the end of the slug automatically. How to do it?
Don't use the hard-coded version that the OP used here. When he did that, there was not a filter available. More recently, since 3.3, a filter was added.
add_filter( 'wp_unique_post_slug', 'custom_unique_post_slug', 10, 4 );
function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if ( $custom_post_type == $post_type ) {
$slug = md5( time() );
}
return $slug;
}
However this method will change the slug every time you save the post... Which was what I was hoping for...
EDIT:
This kind of works for limiting the generation to just once. The only drawback is that it creates one version when ajax runs after creating the title, then it creates another, permanent slug when the post is saved.
function custom_unique_post_slug( $slug, $post_ID, $post_status, $post_type ) {
if ( $custom_post_type == $post_type ) {
$post = get_post($post_ID);
if ( empty($post->post_name) || $slug != $post->post_name ) {
$slug = md5( time() );
}
}
return $slug;
}
Write a plugin to hook into the wp_insert_post_data filter so you can update the slug before the post is sent for insertion into the database:
function append_slug($data) {
global $post_ID;
if (empty($data['post_name'])) {
$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
$data['post_name'] .= '-' . generate_arbitrary_number_here();
}
return $data;
}
add_filter('wp_insert_post_data', 'append_slug', 10);
Note that this function requires that you allow WordPress to auto-generate the slug first, meaning you must not enter your own slug before generating, and it cannot update existing posts with the number.
Test this : (paste it into functions.php)
function append_slug($data) {
global $post_ID;
if (!empty($data['post_name']) && $data['post_status'] == "publish" && $data['post_type'] == "post") {
if( !is_numeric(substr($data['post_name'], -4)) ) {
$random = rand(1111,9999);
$data['post_name'] = sanitize_title($data['post_title'], $post_ID);
$data['post_name'] .= '-' . $random;
}
}
return $data; } add_filter('wp_insert_post_data', 'append_slug', 10);
add_filter('post_link','postLinkFilter', 10, 3);
/**
* Manipulates the permalink
*
* #param string $permalink
* #param stdClass $post
* #return string
*/
function postLinkFilter($permalink,stdClass $post){
return $permalink.'?12345';
}
Untested in that scenario, but I have already used it, should work with a minimum of changes, but try and test it REALLY Carefully .
In any Case, don't use rand() here or something alike, since the function must return the same link for the same post every time, otherwise you will have some serious problems.
Have fun!
You should operate with wp_ajax_sample-permalink action and name_save_pre filter.
More examples here: https://wordpress.stackexchange.com/a/190314/42702