Wordpress: Accesing post title from content_save_pre - wordpress

i modify content when saving and need to access post title, what i have so far in functions.php
add_filter('content_save_pre', 'custom_content_save_pre');
function custom_content_save_pre($s) {
// need to access post title here
$postTitle = ?
// some more code here
return $s;
}

Globalize the $post object and you will have access to it ex:
add_filter('content_save_pre', 'custom_content_save_pre');
function custom_content_save_pre($s) {
global $post;
$postTitle = $post->title;
return $s;
}
but a better hook to use would be wp_insert_post_data which accepts 2 parameters $data and $postarr
where data is an array of the post object that is needed to be returned for saving.

Related

Wordpress REST API: Get raw database content

I want to pull data from my event calender. So I added REST API support to my event content type. But my JSON output is with HTML.
Can I not get the raw database data?
I get this: http://astc.dk/wp-json/wp/v2/event/
I put this in my functions.php
add_action( 'init', 'my_event_cpt', 25 );
function my_event_cpt() {
global $wp_post_types;
//be sure to set this to the name of your post type!
$post_type_name = 'event';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
// Optionally customize the rest_base or controller class
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}

Wordpress PHP warning: Creating default object from empty value in wp-admin\includes\post.php on line 627

I am using Wordpress 4.2.2, Xampp installation in windows with PHP 5.6.8
I have added the following function to my theme's functions.php to automatically set the title of the post type 'departure':
function custom_title() {
if ($_POST['post_type'] == 'departure') :
$post_title = 'Departure-'.$_POST['post_ID'];
return $post_title;
endif;
}
add_filter ( 'title_save_pre', 'custom_title' );
Title setting for the custom post type "departure" works fine. But when I try to add a new wordpress standard post (i.e. post_type = post) I get the following error:
Warning: Creating default object from empty value in wp-admin\includes\post.php on line 627
Line 627 is as follows:
/**
* Filter the default post content initially used in the "Write Post" form.
*
* #since 1.5.0
*
* #param string $post_content Default post content.
* #param WP_Post $post Post object.
*/
$post->post_content = apply_filters( 'default_content', $post_content, $post );
All the other post types (i.e. custom types and wordpress standard page) work fine. By dumping $_POST data in a logfile I have seen, that for all the working post-types an empty array (i.e. array()) is passed as $_POST data. This does not happen with the "post" post type.
If I comment the function in functions.php, an empty array is sent as $_POST data when adding a new "post" post type, and the problem disappears.
Any ideas?
Thanks
You need to return a value, even if the post type isn't departure. The function should take an argument, and return something:
function custom_title($post_title) {
if ($_POST['post_type'] == 'departure') {
$post_title = 'Departure-'.$_POST['post_ID'];
}
return $post_title;
}
add_filter ( 'title_save_pre', 'custom_title' );

Get gravity forms fields

I am using the gravity form on my site. I am working on create the custom report for this I have need gravity form fields name and id based on specific form id.Please let me know how I can do it.
I am using the following function but it is showing all forms info based on it. it is looking very hard to parse it. Please let me know any function so I can get fields name easily.
$forms = RGFormsModel::get_forms_by_id(13);
try this
function get_all_form_fields($form_id){
$form = RGFormsModel::get_form_meta($form_id);
$fields = array();
if(is_array($form["fields"])){
foreach($form["fields"] as $field){
if(isset($field["inputs"]) && is_array($field["inputs"])){
foreach($field["inputs"] as $input)
$fields[] = array($input["id"], GFCommon::get_label($field, $input["id"]));
}
else if(!rgar($field, 'displayOnly')){
$fields[] = array($field["id"], GFCommon::get_label($field));
}
}
}
//echo "<pre>";
//print_r($fields);
//echo "</pre>";
return $fields;
}
It's not that hard to parse:
$fields=array();
foreach ( $forms['fields'] as $field ) {
$fields[]=array($field['id'] => $field['inputName']);
}
P.S. I'm assuming you use Gravity Forms < 1.7 as RGFormsModel::get_forms_by_id is a deprecated function since 1.7
// Get the Form fields
$form = RGFormsModel::get_form_meta($form_id);
// Run through the fields to grab an object of the desired field
$field = RGFormsModel::get_field( $form, $field_id );
I use the above to get a specific field I want to filter the value of. The $field contains an object with all the properties you want.
echo $field->label // Gets the label
echo $field->inputName // Gets the name
echo $field->type // Gets the type
echo $field->cssClass // Gets the CSS Classes as a string
You are able to get the entered value/content of a field by using rgpost() and by referencing the id ($field->id).
// Check the entered value of every field
foreach( $form['fields'] as &$field ) {
// Get the content for the specific field
$fieldContent = rgpost( "input_".$field->id );
// Check the content
if ( $fieldContent == ... ){}
}

What's the difference between global var post and get_post?

I use the_posts filter to add an object to each queried post. When access the added object, I get different result by using $post or get_post.
This is the code to attach the object to posts:
add_filter( 'the_posts', 'populate_posts_obj', 10,2 );
function populate_posts_obj( $posts, $query ){
if ( !count( $posts ) || !isset($query->query['post_type']) )
return $posts;
if( in_array( $query->query['post_type'], get_valid_grade_types())){
foreach ( $posts as $post ) {
if ( $obj = new Gradebook( $post->ID ) )
$post->gradebook = $obj;
}
}
return $posts;
}
Then, access the obj via $post, sometimes get the obj, sometimes not (even when it's the same post):
function get_the_gradebook(){
global $post;
return isset($post->gradebook) ? $post->gradebook : null;
}
Access the obj via get_post(), always get the obj:
function get_the_gradebook(){
global $post;
$p = get_post($post->ID);
return isset($p->gradebook) ? $p->gradebook : null;
}
I can just use the get_post() version, but it would be useful if I know why the difference.
Additional info:
If you ask the reason I attach an obj to each post, I think WordPress may take care of the caching process at the first place. Then, other caching plugins can work on my obj as if working on standard WP posts.
Lets explain you with a little bit pseudo code. I am trying to be broad with my approach so that my answer is relevant to StackOverflow however I still don't know how many down votes I may be receiving for this.
The simple difference is $post is a variable and get_post() is a method that means you can expect a different output from get_post() due to several dependencies however $post will only change when you explicitly do that.
Lets assume something like this
function get_post() {
return rand(0, 5);
}
$post = get_post(); /* lets assume random
value that was generated
this time was "2" */
Now each time you call get_post() its value be keep changing however the value of $post is always 2.
Coming back to the context of wordpress, $post is set using get_post() within the Loop and corresponds to the object referring to default post ID for current URL where as get_post() will take post ID as an input and return the post object.
$post is what WordPress considers to be the current "post" (post/page/custom post type) and can quite often end up giving you data you didn't quite expect. This is especially true if you perform WP_Query's in your template or have a template that uses data from several "posts".
By using get_post() with the ID you want the data from, you can be assured that you are getting the data you really want.

Search posts by custom fields using xml-rpc

Is it possible? Or is there any other way to do it without xml-rpc?
Yes, it is possible, but you have to write your own xml-rpc method.
For example:
add_filter( 'xmlrpc_methods', 'my_add_xmlrpc_methods' );
//this function adds new xml-rpc method
//my.search_posts - name of xml-rpc method
//search_posts_by_custom_field - name of the function that should proccess my.search_posts method call
function my_add_xmlrpc_methods( $methods ) {
$methods['my.search_posts'] = 'search_posts_by_custom_field';
return $methods;
}
//this function actually search posts
function search_posts_by_custom_field($args) {
//authenticate user
//run search DB query
//return results
}

Resources