How To make an API in Wordpress - php

This is my first time i use wordpress for create apps. in there i want to make an api but dont know how to make it in wordpress.
this my code in website :
function drivers_post_type() {
$labels = array(
'name' => _x( 'driver', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'driver', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'driver', 'text_domain' ),
'name_admin_bar' => __( 'Post Type', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Items', 'text_domain' ),
'add_new_item' => __( 'Add New Item', 'text_domain' ),
'add_new' => __( 'Add New', 'text_domain' ),
'new_item' => __( 'New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
);
$args = array(
'label' => __( 'driver', 'text_domain' ),
'description' => __( 'driver', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title','thumbnail' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => array('slug' => 'driver'),
'capability_type' => 'page',
);
register_post_type( 'drivers', $args );
}
// Hook into the 'init' action
add_action( 'init', 'drivers_post_type', 0 );
// Little function to return a custom field value
function driverMB_get_custom_field( $value ) {
global $post;
$custom_field = get_post_meta( $post->ID, $value, true );
if ( !empty( $custom_field ) )
return is_array( $custom_field ) ? stripslashes_deep( $custom_field ) : stripslashes( wp_kses_decode_entities( $custom_field ) );
return false;
}
// Register the Metabox
function driverMB_add_custom_meta_box() {
add_meta_box(
'driverMB-meta-box',
__( 'driver Info', 'textdomain' ),
'driverMB_meta_box_output',
'drivers',
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'driverMB_add_custom_meta_box' );
// Output the Metabox
function driverMB_meta_box_output( $post ) {
// create a nonce field
wp_nonce_field( 'my_driverMB_meta_box_nonce', 'driverMB_meta_box_nonce' ); ?>
<p>
<label><b>ID</b></label>
<label><?php echo get_the_ID() ?></label>
</p>
<p>
<label><b>Username</b></label>
<input type="text" placeholder="Username" name="username" id="username" value="<?php echo driverMB_get_custom_field( 'username' ); ?>" style="width:100%;" />
</p>
<p>
<label><b>Password</b></label>
<input type="password" placeholder="Password" name="password" id="password" value="<?php echo driverMB_get_custom_field( 'password' ); ?>" style="width:100%;" />
</p>
<p>
<label><b>Email</b></label>
<input type="text" placeholder="Email" name="email" id="email" value="<?php echo driverMB_get_custom_field( 'email' ); ?>" style="width:100%;" />
</p>
<p>
<label><b>Phone Number</b></label>
<input type="text" placeholder="Ext : 088216192560" name="phone" id="phone" value="<?php echo driverMB_get_custom_field( 'phone' ); ?>" style="width:100%;" />
</p>
<?php
}
// Save the Metabox values
function driverMB_meta_box_save( $post_id ) {
// Stop the script when doing autosave
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// Verify the nonce. If insn't there, stop the script
if( !isset( $_POST['driverMB_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['driverMB_meta_box_nonce'], 'my_driverMB_meta_box_nonce' ) ) return;
// Stop the script if the user does not have edit permissions
if( !current_user_can( 'edit_post' ) ) return;
// Save the textfield
if( isset( $_POST['username'] ) )
update_post_meta( $post_id, 'username', esc_attr( $_POST['username'] ) );
if( isset( $_POST['password'] ) )
update_post_meta( $post_id, 'password', esc_attr( $_POST['password'] ) );
if( isset( $_POST['email'] ) )
update_post_meta( $post_id, 'email', esc_attr( $_POST['email'] ) );
if( isset( $_POST['phone'] ) )
update_post_meta( $post_id, 'phone', esc_attr( $_POST['phone'] ) );
}
add_action( 'save_post', 'driverMB_meta_box_save' );
i dont know how to create it as api. my friend told me to create if url equals then perform action function, but i dont know how ?
Does someone tell me or help me to create an api for wordpress ?

No doubt there are many other plugins in the market too but these are the two which I used personally for creating APIs in WordPress:
Using WP Rest API (Now a days it is officially supported by WordPress):
The detailed documentation for the same is provided here.
Using Json API plugin (for simplicity). If you use this plugin then it will allow you to create your "Controller", "Model" etc files and hence you can write your custom endpoints. You can check more details here.
Note : I have used this plugin last year and it works great.

To answer your question, I must say:
You must first create a folder in the mainstream WordPress path and place your project files in it.
enter image description here
Then add your original file with extension .php Put it in it.
Note that you need to move your code based on the function.

Related

Wordpress - Add custom field to custom post created programmatically

I have created a custom plugin on Wordpress that creates a custom post on installation.
The code I use is as follows:
function custom_post_type() {
$labels = array(
'name' => _x( 'Books', 'Post Type General Name', 'my-custom-domain' ),
'singular_name' => _x( 'Book', 'Post Type Singular Name', 'my-custom-domain' ),
'menu_name' => __( 'Book', 'my-custom-domain' ),
'parent_item_colon' => __( 'Parent Book', 'my-custom-domain' ),
'all_items' => __( 'All Books', 'my-custom-domain' ),
'view_item' => __( 'Show Book', 'my-custom-domain' )
);
$args = array(
'label' => __( 'Books', 'my-custom-domain' ),
'description' => __( 'Books and news', 'my-custom-domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'genres' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
register_post_type( 'libri', $args );
}
add_action( 'init', 'custom_post_type', 0 );
What I would like to do is to be able to create a custom field (again from code) to be inserted under this function and add it to the post created above.
A custom field such as a text field with a title and a label box in which to insert text, or a field consisting of two text boxes.
How can I create it in code and add it to the post so that when I do "Add new" I get this field to fill in?
I mean something like THIS.
There's some really great tutorials that cover this. My answer is taken from this one written by Aileen Javier from WPMU Dev
It's a 3 step process to create custom metaboxes on a page. The first step is to create the metabox using the add_meta_box() function
function libri_add_meta_box( $post ){
add_meta_box( 'libri_meta_box', 'Book Attributes', 'libri_build_meta_box', 'libri', 'side', 'low' );
}
add_action( 'add_meta_boxes_libri', 'libri_add_meta_box' );
The next step is to create a function which we use as the callback parameter in our add_meta_box (in this case 'libri_build_meta_box') - We use this to create inputs for our custom fields. In this instance a text field for Book Author.
function libri_build_meta_box( $post ){
wp_nonce_field( basename( __FILE__ ), 'libri_meta_box_nonce' );
$book_author = get_post_meta( $post->ID, '_book_author', true); ?>
<div class='inside'>
<h3>Book Author</h3>
<p>
<input type="text" name="book_author" value="<?php echo $book_author; ?>" />
</p>
</div>
<?php }
And the final step is to hook into the save_post_{$post->post_type} action, to take the request and then update the post_meta when the post is saved
function libri_save_meta_box_data( $post_id ){
if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
if ( isset( $_REQUEST['book_author'] ) ) {
update_post_meta( $post_id, '_book_author', sanitize_text_field( $_POST['book_author'] ) );
}
}
add_action( 'save_post_libri', 'libri_save_meta_box_data', 10, 2 );
This will create a metabox on the side of the page that has a single text input for a book author. I'd really recommend reading the whole tutorial from wpmudev, because it's really informative and helpful.
Here's the full code from my example:
<?php function libri_add_meta_box( $post ){
add_meta_box( 'libri_meta_box', 'Book Attributes', 'libri_build_meta_box', 'libri', 'side', 'low' );
}
add_action( 'add_meta_boxes_libri', 'libri_add_meta_box' );
function libri_build_meta_box( $post ){
wp_nonce_field( basename( __FILE__ ), 'libri_meta_box_nonce' );
$book_author = get_post_meta( $post->ID, '_book_author', true); ?>
<div class='inside'>
<h3>Book Author</h3>
<p>
<input type="text" name="book_author" value="<?php echo $book_author; ?>" />
</p>
</div>
<?php }
function libri_save_meta_box_data( $post_id ){
if ( !isset( $_POST['food_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['food_meta_box_nonce'], basename( __FILE__ ) ) ){
return;
}
if ( isset( $_REQUEST['book_author'] ) ) {
update_post_meta( $post_id, '_book_author', sanitize_text_field( $_POST['book_author'] ) );
}
}
add_action( 'save_post_libri', 'libri_save_meta_box_data', 10, 2 );

Custom Post Type Won't Output Post

I'm creating a plugin and I have a custom post type set up that includes meta boxes, custom fields and custom taxonomies. All that is working fine and on my wordpress dashboard, the "add new post" is all there. However when I try to "save draft" or "publish" a post, it's not working. Error included below.
Here's the code for my CPT.
// Register Custom Post Type
function post_type_book() {
$labels = array(
'name' => _x( 'All Books', 'book post type name', 'author_station' ),
'singular_name' => _x( 'Book', 'Singular book post type name', 'author_station' ),
'menu_name' => __( 'Books', 'author_station' ),
'name_admin_bar' => __( 'Post Type', 'author_station' ),
'archives' => __( 'Item Archives', 'author_station' ),
'attributes' => __( 'Item Attributes', 'author_station' ),
'parent_item_colon' => __( 'Parent Item:', 'author_station' ),
'all_items' => __( 'All Items', 'author_station' ),
'add_new_item' => __( 'Add New Book', 'author_station' ),
'add_new' => __( 'Add New', 'author_station' ),
'new_item' => __( 'New Book', 'author_station' ),
'edit_item' => __( 'Edit Book', 'author_station' ),
'update_item' => __( 'Update Item', 'author_station' ),
'view_item' => __( 'View Book', 'author_station' ),
'view_items' => __( 'View Items', 'author_station' ),
'search_items' => __( 'Search Item', 'author_station' ),
'not_found' => __( 'Not books found', 'author_station' ),
'not_found_in_trash' => __( 'Not books found in Trash', 'author_station' ),
'featured_image' => __( 'Book Cover Image', 'author_station' ),
'set_featured_image' => __( 'Set Book Cover Image', 'author_station' ),
'remove_featured_image' => __( 'Remove Book Cover Image', 'author_station' ),
'use_featured_image' => __( 'Use as Book Cover Image', 'author_station' ),
'insert_into_item' => __( 'Insert into item', 'author_station' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'author_station' ),
'items_list' => __( 'Items list', 'author_station' ),
'items_list_navigation' => __( 'Items list navigation', 'author_station' ),
'filter_items_list' => __( 'Filter items list', 'author_station' ),
);
$args = array(
'label' => __( 'book', 'author_station' ),
'description' => __( 'Custom Book Entry', 'author_station' ),
'supports' => array('title'),
'taxonomies' => array( 'genres', 'series', 'keywords' ),
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'menu_position' => 20,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'rewrite' => array( 'slug' => 'as_book' ),
);
register_post_type( 'as_book', $args );
}
add_action( 'init', 'post_type_book');
function save_as_book( $post_id ) {
// verify nonce
if ( !wp_verify_nonce( $_POST['your_as_book_nonce'], basename(__FILE__) ) ) {
return $post_id;
}
// check autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// check permissions
if ( 'page' === $_POST['as_book'] ) {
if ( !current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
} elseif ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
$old = get_post_meta( $post_id, 'as_book', true );
$new = $_POST['as_book'];
if ( $new && $new !== $old ) {
update_post_meta( $post_id, 'as_book', $new );
} elseif ( '' === $new && $old ) {
delete_post_meta( $post_id, 'as_book', $old );
}
}
add_action( 'save_post', 'save_as_book' );
This is the error that comes up when I try to 'save draft' or 'publish' a post.
Fatal error: Call to a member function get_queried_object_id() on null in ..../wp-includes/query.php on line 60

Custom Wordpress Taxonomy (Actors), show today's birthdays on homepage with query

I have a movie website, with movies as posts, and actors as a custom taxonomy. For my custom taxonomy, I added this in my functions.php file:
$labels = array(
'name' => _x( 'Actors', 'taxonomy general name', 'textdomain' ),
'singular_name' => _x( 'Actor', 'taxonomy singular name', 'textdomain' ),
'search_items' => __( 'Search Actors', 'textdomain' ),
'popular_items' => __( 'Popular Actors', 'textdomain' ),
'all_items' => __( 'All Actors', 'textdomain' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Actor', 'textdomain' ),
'update_item' => __( 'Update Actor', 'textdomain' ),
'add_new_item' => __( 'Add New Actor', 'textdomain' ),
'new_item_name' => __( 'New Actor Name', 'textdomain' ),
'separate_items_with_commas' => __( 'Separate actors with commas', 'textdomain' ),
'add_or_remove_items' => __( 'Add or remove actors', 'textdomain' ),
'choose_from_most_used' => __( 'Choose from the most used actors', 'textdomain' ),
'not_found' => __( 'No actors found.', 'textdomain' ),
'menu_name' => __( 'Actors', 'textdomain' ),
);
$args = array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'actor' ),
);
register_taxonomy( 'actor', 'post', $args );
I have added a field to the actor taxonomy with their date of birth (in yyyy-mm-dd format) in my functions.php file:
function crq_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[starbday]"><?php _e( 'Birthdate', 'responsivo' ); ?></label>
<input type="text" name="term_meta[starbday]" id="term_meta[starbday]" value="">
<p class="description"><?php _e( 'Enter Birthdate (in <em>yyyy-mm-dd</em> format)','responsivo' ); ?></p>
</div>
<?php }
add_action( 'actor_add_form_fields', 'crq_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function crq_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" );
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="term_meta[starbday]"><?php _e( 'Birthdate', 'responsivo' );
?>
</label>
</th>
<td>
<input type="text" name="term_meta[starbday]" id="term_meta[starbday]" value="<?php echo esc_attr( $term_meta['starbday'] ) ? esc_attr( $term_meta['starbday'] ) : ''; ?>">
<p class="description"><?php _e( 'Enter Birthdate (in <em>yyyy-mm-dd</em> format)','responsivo' ); ?></p>
</td> </tr>
add_action( 'actor_edit_form_fields', 'crq_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}}
add_action( 'edited_actor', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_actor', 'save_taxonomy_custom_meta', 10, 2 );
I am able to show this field on the actor's taxonomy page with the following code in the taxonomy template:
<?php
$birthd = $term_meta['starbday'];
echo date('F j, Y', strtotime($birthd));
?>
Now, I am trying to run a query to show today's birthdays on the homepage.
This is what I have come up with so far.
<ul>
<?php
$todaymd = date('m-d');
$args = array(
'taxonomy' => 'actor',
'orderby' => 'name'
'order' => 'ASC',
'hide_empty' => false,
'meta_query' => array(
array(
'key' => 'starbday',
'value' => '_____'.$todaymd,
'compare' => 'LIKE'
)
)
);
$terms = get_terms( 'actor', $args );
foreach( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
This isn't working at all, and nothing is displayed. While this would be the case if there were no birthdays on today's date, even if I change $todaymd to '03-15' (which I know is the birth month and day of several actors), it still displays nothing. Any suggestions?
I saw something similar asked here...https://wordpress.stackexchange.com/questions/82323/how-can-i-get-the-actor-birthday-by-date, but that wasn't answered, as the solution didn't work.
Actually, Sally J, I figured it out, thanks to https://www.wphub.com/blog/posts/adding-metadata-taxonomy-terms/ (whoops! wrong link! see addition below) for the functions.php section and updating the homepage with this:
<ul>
<?php
$todaymd = date('m-d');
$args = array(
'taxonomy' => 'actor',
'orderby' => 'name',
'order' => 'ASC',
);
$terms = get_terms( $args );
foreach( $terms as $term ) {
$starbday = get_term_meta($term->term_id, 'starbday', true);
if((substr($starbday,5,5)) == $todaymd) { ?>
<li><?php echo $term->name; ?></li>
<?php } else { }} ?>
</ul>
It does work now, but it doesn't seem to be the most efficient way (since I am pulling all the actors then filtering them, rather than filtering while getting them), but it works.
Thank you for your help!
UPDATE Since I put in the wrong link above, this is what is in my functions.php now:
// Add term page for Actor, Director and Book Author Taxonomies
add_action('actor_add_form_fields', 'crq_metabox_add', 10, 1);
add_action('actor_edit_form_fields', 'crq_metabox_edit', 10, 1);
function crq_metabox_add($tag) {
?>
<div class="form-field">
<label for="crq_starbday"><?php _e( 'Birthdate', 'responsivo' ); ?></label>
<input type="text" name="crq_starbday" id="crq_starbday" value="">
<p class="description"><?php _e( 'Enter Birthdate (in <em>yyyy-mm-dd</em> format)','responsivo' ); ?></p>
</div>
<?php }
// Edit term page for Actor, Director and Book Author Taxonomies
function crq_metabox_edit($tag) {
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="crq_starbday"><?php _e( 'Birthdate', 'responsivo' ); ?></label>
</th>
<td>
<input type="text" name="crq_starbday" id="crq_starbday" value="<?php echo get_term_meta($tag->term_id, 'crq_starbday', true); ?>" />
<p class="description"><?php _e( 'Enter Birthdate (in <em>yyyy-mm-dd</em> format)','responsivo' ); ?></p>
</td>
</tr>
<?php
}
// Save extra fields for Actor, Director and Book Author Taxonomies
add_action( 'created_actor', 'save_taxonomy_metadata', 10, 1 );
add_action( 'edited_actor', 'save_taxonomy_metadata', 10, 1 );
function save_taxonomy_metadata($term_id)
{
if (isset($_POST['crq_starbday'])) {
update_term_meta( $term_id, 'crq_starbday', $_POST['crq_starbday']);
}
}
...which uses the new(er) update_term_meta(). I tried using meta_query() on the homepage, but it didn't display any results (probably because $todaymd and starbday are so different (starbday is yyyy-mm-dd, while $todaymd is just mm-dd). And the '_____'.$todaymd value doesn't work, either.

Wordpress plugin Advanced Custom Fields Pro not saving Frontend and Backend

I am using A custom Post Type with Advanced Custom Fields to create custom entries of a post. My Customs Post Type and Options are added with (please scroll on codebox to see all):
add_action( 'init', 'register_cpt_campaigns' );
function register_cpt_campaigns() {
$labels = array(
'name' => __( 'Campaigns', 'campaigns' ),
'singular_name' => __( 'campaign', 'campaigns' ),
'add_new' => __( 'Add New', 'campaigns' ),
'add_new_item' => __( 'Add New Campaign', 'campaigns' ),
'edit_item' => __( 'Edit Campaign', 'campaigns' ),
'new_item' => __( 'New Campaign', 'campaigns' ),
'view_item' => __( 'View Campaign', 'campaigns' ),
'search_items' => __( 'Search Campaigns', 'campaigns' ),
'not_found' => __( 'Campaign not found', 'campaigns' ),
'not_found_in_trash' => __( 'Campaign not found', 'campaigns' ),
'parent_item_colon' => __( 'Parent campaign:', 'campaigns' ),
'menu_name' => __( 'Campaigns', 'campaigns' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Custom post type for Discovr Campaigns',
'supports' => array( 'author','title' ),
'taxonomies' => array( 'campaign_category', 'campaign_action' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-welcome-widgets-menus',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'campaigns', $args );
}
And custom Taxonomies with (please scroll on codebox to see all):
// Register Campaign Type Taxonomy
function campaign_post_category() {
$labels = array(
'name' => _x( 'Campaign Types', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Campaign Type', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Campaign Types', 'text_domain' ),
'all_items' => __( 'All types', 'text_domain' ),
'parent_item' => __( 'Parent types', 'text_domain' ),
'parent_item_colon' => __( 'Parent types:', 'text_domain' ),
'new_item_name' => __( 'New Campaign Type', 'text_domain' ),
'add_new_item' => __( 'Add New Campaign Type', 'text_domain' ),
'edit_item' => __( 'Edit Campaign Type', 'text_domain' ),
'update_item' => __( 'Update Campaign Type', 'text_domain' ),
'view_item' => __( 'View Campaign Type', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate campaign types with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove campaign types', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular campaign types', 'text_domain' ),
'search_items' => __( 'Search campaign type', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No campaign types', 'text_domain' ),
'items_list' => __( 'Campaign type list', 'text_domain' ),
'items_list_navigation' => __( 'Campaign type list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => false,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
);
register_taxonomy( 'campaign_category', array( 'campaign_categories' ), $args );
}
add_action( 'init', 'campaign_post_category', 0 );
// Register Campaign Status Taxonomy
function campaign_post_action() {
$labels = array(
'name' => _x( 'Campaign Status', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'Campaign Status', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Campaign Status', 'text_domain' ),
'all_items' => __( 'All Status', 'text_domain' ),
'parent_item' => __( 'Parent Status', 'text_domain' ),
'parent_item_colon' => __( 'Parent Status:', 'text_domain' ),
'new_item_name' => __( 'New Campaign Status', 'text_domain' ),
'add_new_item' => __( 'Add New Campaign Status', 'text_domain' ),
'edit_item' => __( 'Edit Campaign Status', 'text_domain' ),
'update_item' => __( 'Update Campaign Status', 'text_domain' ),
'view_item' => __( 'View Campaign Status', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate campaign status with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove campaign status', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular campaign status', 'text_domain' ),
'search_items' => __( 'Search campaign status', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No campaign status', 'text_domain' ),
'items_list' => __( 'campaign status list', 'text_domain' ),
'items_list_navigation' => __( 'campaign status list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => false,
'show_admin_column' => true,
'show_in_nav_menus' => false,
'show_tagcloud' => false,
);
register_taxonomy( 'campaign_action', array( 'campaign_actions' ), $args );
}
add_action( 'init', 'campaign_post_action', 0 );
I also have some functions that are added to the theme to support my functionality of the post-type. I have removed these to rectify my issue but did not fix anything.
// Add Default Campaign Status
function set_default_campaign_status( $post_id, $post ) {
if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
$defaults = array(
'campaign_action' => array( 'Draft' )
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'set_default_campaign_status', 0, 2 );
// Add Default Campaign Type
function set_default_campaign_type( $post_id, $post ) {
if ( 'publish' === $post->post_status && $post->post_type === 'campaigns' ) {
$defaults = array(
'campaign_category' => array( 'Not Selected' )
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'set_default_campaign_type', 0, 2 );
// Delete post
function delete_post(){
global $post;
$deletepostlink= add_query_arg( 'frontend', 'true', get_delete_post_link( get_the_ID() ) );
if (current_user_can('edit_post', $post->ID)) {
echo '<button class="m-t-10 m-b-10 btn btn-danger btn-cons" type="button">Delete</button>';
}
}
//Redirect after delete post in frontend
add_action('trashed_post','trash_redirection_frontend');
function trash_redirection_frontend($post_id) {
if ( filter_input( INPUT_GET, 'frontend', FILTER_VALIDATE_BOOLEAN ) ) {
wp_redirect( get_option('siteurl').'/campaigns' );
exit;
}
}
// Add default Campaign Pages
function discovr_campaign_endpoints() {
add_rewrite_endpoint( 'overview', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-details', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-audience', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-page', EP_PERMALINK );
add_rewrite_endpoint( 'new-campaign-ads', EP_PERMALINK );
add_rewrite_endpoint( 'edit-campaign', EP_PERMALINK );
add_rewrite_endpoint( 'analytics', EP_PERMALINK );
}
add_action( 'init', 'discovr_campaign_endpoints' );
// Update Campaign Page on Save
add_action( 'save_post', 'wpse105926_save_post_callback' );
function wpse105926_save_post_callback( $post_id ) {
// verify post is not a revision
if ( ! wp_is_post_revision( $post_id ) ) {
// unhook this function to prevent infinite looping
remove_action( 'save_post', 'wpse105926_save_post_callback' );
// update the post slug
wp_update_post( array(
'ID' => $post_id,
'post_name' => '' // do your thing here
));
// re-hook this function
add_action( 'save_post', 'wpse105926_save_post_callback' );
}
}
// Add Campaign Title Label and Instuctions
function my_acf_prepare_field( $field ) {
$field['label'] = "Campaign Title";
$field['instructions'] = "Internal use only. Ex: `Retarget Abandoned Bookings`";
return $field;
}
add_filter('acf/prepare_field/name=_post_title', 'my_acf_prepare_field');
Unfortunately, everytime I save a custom field it reverts to empty or as it's original. This only happens with the ACF fields and not with fields such as title, author or taxonomies not overwritten with ACF. Please see the following gif images to understand further.
Backend of Wordpress with Post being created and updated
ACF Custom Fields Group Settings
Frontend Post being created
I have tried removing all frontend funtionality but still doesn't work in the backend, also removed all plugins either than ACF. No error in debug or firebug. I can only imaging that there is a conflict in the fields, or ACF is not able to find the appropriate place to save these.
Any help is greatly appreciated and I hope that there is something simple that I have missed.
UPDATE
This works by changing the field_name and field_label to something obscure.
ACF Field Group working
Frontend form saving
Getting this to now work is great, however, I need the field_label and field_name to match the purpose of these fields. I could overwrite these within the functions.php but this will be a nightmare as we potentially have 20 fields to add.
If you change the field_label to what is the purpose of this campaign and the field_name to what_is_the_purpose_of_this_campaign it does not work.
However, if you change the field_label` to whatisthepurposeofthiscampaign and the field_name to whatisthepurposeofthiscampaign it does work.
It seems that any spaces break the connection of the field_label and field_name
This error was created by permissions problems with certain plugins. I had uploaded via FTP as root, whereas Wordpress was using www-data as the file owner. I matched the permissions through the console and this has been fixed.
Thank you for your help.

put custom field without using custom fields

I just need the extra fields on my post but without using advance custom fields.
Please what should i do the changes in my function.php
i have simple do the below code for custom post i need on extra field which name should be "company" what changes i required in below code.
please can anyone tell me ?
add_action( 'init', 'client' );
function client() {
register_post_type( 'client',
array(
'labels' => array(
'name' => __( 'Our Client' ),
'singular_name' => __( 'client' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'client'),
'supports' => array( 'title','thumbnail')
)
);
}
Try the following code:
add_action( 'init', 'client' );
function client() {
register_post_type( 'client',
array(
'labels' => array(
'name' => __( 'Our Client' ),
'singular_name' => __( 'client' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'client'),
'supports' => array( 'title','thumbnail'),
'register_meta_box_cb' => 'add_client_metaboxes',
)
);
}
//Code for meta box.
add_action( 'add_meta_boxes', 'add_client_metaboxes' );
function add_client_metaboxes() {
add_meta_box('company_description', 'Company', 'company_description', 'client');
}
function company_description() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="itemmeta_noncename" id="itemmeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
// Get the location data if its already been entered
$comapny_detail = get_post_meta($post->ID, 'comapny_detail', true);
// Echo out the field
echo '<input type="text" name="comapny_detail" value="'.$comapny_detail.'">';
}
Edited:
function wpt_client() {
register_post_type( 'client',
array(
'labels' => array(
'name' => __( 'Clients' ),
'singular_name' => __( 'Client' ),
'add_new' => __( 'Add New Client' ),
'add_new_item' => __( 'Add New Client' ),
'edit_item' => __( 'Edit Client' ),
'new_item' => __( 'Add New Client' ),
'view_item' => __( 'View Client' ),
'search_items' => __( 'Search Client' ),
'not_found' => __( 'No cleint found' ),
'not_found_in_trash' => __( 'No client found in trash' )
),
'public' => true,
'supports' => array( 'title','editor','thumbnail'),
'capability_type' => 'post',
'rewrite' => array("slug" => "client"), // Permalinks format
'menu_position' => 20,
'register_meta_box_cb' => 'create_meta_boxes',
)
);
}
add_action( 'init', 'wpt_client' );
/* Custom meta boxes */
add_action( 'add_meta_boxes', 'create_meta_boxes' );
function create_meta_boxes() {
add_meta_box( 'my-meta-box-id', __('Company Name'), 'client_info', 'client', 'normal', 'low' );
}
// Create meta box: Company Name
function client_info( $post ) {
$values = get_post_custom( $post->ID );
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<?php $text = get_post_meta($post->ID, 'client_info', true); ?>
<input type="text" name="client_info" id="client_info" style="width: 100%; margin: 6px 0;" value="<?php echo $text; ?>" />
<?php
}
// Save meta box: Company Name
add_action( 'save_post', 'save_client_info' );
function save_client_info( $post_id ) {
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
if( isset( $_POST['client_info'] ) )
update_post_meta( $post_id, 'client_info', wp_kses( $_POST['client_info'], $allowed ) );
}

Categories