How to Get visual composer page through rest api - wordpress

I'm developing an ionic app in which I have a rest api to get the blog contents from wordpress page. I'm trying to get the visual composer page through api but I'm getting raw html format and css or missing for visual component pages. Can I get the visual composer page fully along the properties through api or is there any restriction to use api ??

I think you find your answer here: https://github.com/WP-API/WP-API/issues/2578
The example below was taken from the link above (thank you, bradmsmith!)
Here is an example how to render the VC shortcodes on the content of a post:
add_action( 'rest_api_init', function ()
{
register_rest_field(
'page',
'content',
array(
'get_callback' => 'compasshb_do_shortcodes',
'update_callback' => null,
'schema' => null,
)
);
});
function compasshb_do_shortcodes( $object, $field_name, $request )
{
WPBMap::addAllMappedShortcodes(); // This does all the work
global $post;
$post = get_post ($object['id']);
$output['rendered'] = apply_filters( 'the_content', $post->post_content );
return $output;
}

Related

Woocommerce Rest APi get custom meta field

I'm trying to pull a products custom meta and custom taxonomy value from woocommerce using the rest api. But no matter what I try a can't seem to get it to work. Not sure what I'm missing.
Example, my products have a custom meta value "product_location". The post meta value exists and works because I can use this to pull the value into the woocommerce product page:
get_post_meta($product->get_id(), 'product_location', true );
In my functions.php file I've added:
add_action( 'rest_api_init', 'handle_location' );
function handle_location() {
register_rest_field( 'post', 'product_location', array(
'get_callback' => array( $this, 'get_the_product_location' ),
'schema' => null
));
}
function get_the_product_location( $post, $field_name, $request ) {
return get_post_meta( $post[ 'id' ], $field_name, true );
}
However, when I make a call say https://*******.com/wc-api/v2/products/302341/ the meta data isn't included. So basically how can I include the additional meta data? I plan on trying this for a custom taxonomy as well. Any help is appreciated - Thanks
Try changing your bottom function to
function get_the_product_location( $post, $field_name, $request ) {
return get_post_meta( $post[ 'id' ], 'product_location', $meta_value );
}

Post a WordPress post on post request

How can I post a post on WordPress through a post request without using the UI?
If possible I would also like to have guidance on how I can implement this with ACF fields.
You could create a child theme or a plugin to write your custom functionality.
You can handle AJAX requests with a specific action, and call wp_insert_post() to create posts.
Example to get you started:
add_action( 'wp_ajax_create_post', 'create_post_ajax_handler' );
/**
* Handle the create post ajax request
*/
function create_post_ajax_handler() {
// Get the post title from the ajax request
// You can get whatever you have passed here
// Also, perform any validations you might want
$post_title = $_POST['post_title'];
// Create the post
$post_id = wp_insert_post( array(
'post_title' => $post_title,
'post_status' => 'publish'
// you could also specify the 'post_type', 'meta_input' etc
), true );
// Error handling
if ( is_wp_error( $post_id ) ) {
// Send error response
wp_send_json_error( $post_id->get_error_message() );
}
// Send success response
wp_send_json_success( $post_id );
}
add_action( 'wp_enqueue_scripts', 'enqueue_ajax_script' );
/**
* Enqueue the ajax script
*/
function enqueue_ajax_script() {
// Enqueue your JavaScript file with 'jquery' as a dependency
wp_enqueue_script(
'ajax-script',
plugin_dir_url( __FILE__ ) . 'ajax-script.js',
array( 'jquery' )
);
// Expose the url to admin-ajax.php as `ajax_object.ajaxurl`
wp_localize_script(
'ajax-script',
'ajax_object',
array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) )
);
}
// Set the url as `ajax_object.ajaxurl` which is the url to admin-ajax.php
$.ajax(ajax_object.ajaxurl, {
method: 'POST',
data: {
// Your action should match the name of your 'wp_ajax_{action}' hook
action: 'create_post',
// Pass any data you want
post_title: 'Example post title'
}
})
.done((response) => {
// Do whatever you want with the response (in this example, this would be the post id)
console.log(response);
})
.fail((error) => {
// Handle the errors
console.error(error);
});
You might also want to look into Nonces, which help protect against Cross-Site Request Forgery (CSRF).
Disclaimer: I haven't tested this code, but I hope you get the idea.
Edit: Since you mentioned that you use Advanced Custom Fields:
$post_id = wp_insert_post( array(
'post_title' => $post_title,
'post_status' => 'publish',
// You can set ACF fields in the 'meta_input' array
'meta_input' => array(
'acf_custom_field_name' => 'an example value'
)
), true );
Edit #2: Please read more about AJAX in WordPress on the Codex.
Replying to your comment:
to which URL should I make the ajax request in order for it to trigger?
On the Codex, under AJAX in Plugins > Ajax on the Viewer-Facing Side
You might also use wp_localize_script() to make the URL available to your script, and generate it using this expression: admin_url( 'admin-ajax.php' )
how do I insert the action "wp_ajax_create_post" in my post request?
On the Codex, AJAX in Plugins > Ajax on the Administration Side
Notice how the 'action' key's value 'my_action', defined in our JavaScript above, matches the latter half of the action 'wp_ajax_my_action' in our AJAX handler below.

Wordpress REST API for favicon and logo images

Can someone help me how to get to the endpoint for logo and favicon images in Wordpress rest api? When I use http:// .... / wp-json / endpoint, I'm getting the title or name and description with no links to images?
For logo you need add this function to functions.php:
// wordpress api for logo extension
add_action( 'rest_api_init', 'add_logo_to_JSON' );
function add_logo_to_JSON() {
register_rest_field( 'post', 'page_logo_src', array( // post for where to register - page_logo_src is the name for api
'get_callback' => 'get_logo_src',
'update_callback' => null,
'schema' => null,
)
);
}
function get_logo_src( $object, $field_name, $request ) {
$size = 'full';
$custom_logo_id = get_theme_mod( 'custom_logo' );
$feat_img_array = wp_get_attachment_image_src($custom_logo_id, $size, true);
return $feat_img_array[0];
}
Then logo is available here: http://your-domain/wp-json/wp/v2/posts under page_logo_src.
You can use this function to grab any of wordpress data you need, for favicon use get_site_icon_url.

Wordpress Profile File / Page

I would like to create a file in the Wordpress theme, where i will add my own code, edit profile, show profile information, and perhaps an ability to insert posts / meta data programmatically.
So it needs to be www.mysite.com/profile.php or www.mysite.com/profile/
I do not want to use Buddy Press or any other plugin.
I know how the template system works, i do not want a page template.
It will probably be a class, later on, i do not want to change .htaccess file, and if i must i would appreciated filter function how to do this from functions.php
Basically just a simple .php file i can link to, located in theme root.
include('../../../wp-load.php');
and write any code i would like to.
Any creative solution that is not too "hacky" would be appreciated.
Spent around 2 days googling bashing my head on this, before i decided to ask question.
Thank you very much.
Ok, I managed to do this, took me 2 days to figure it out. Here is how I managed to do it:
Make a plugin folder.
In that plugin folder make 1x php file. so index.php
Ok so first thing we need to register plugin I did it like this, in your index.php paste
this code.
function activate_profile_plugin() {
add_option( 'Activated_Plugin', 'Plugin-Slug' );
/* activation code here */
}
register_activation_hook( __FILE__, 'activate_profile_plugin' );
Then we need a function when you register a plugin only once register profile pages.
function create_profile_page( $title, $slug, $post_type, $shortcode, $template = null ) {
//Check if the page with this name exists.
if(!get_page_by_title($title)) {
// if not :
$page_id = -1;
$page_id = wp_insert_post(
array(
'comment_status' => 'open',
'ping_status' => 'open',
'post_content' => $shortcode,
'post_author' => 1, // Administrator is creating the page
'post_title' => $title,
'post_name' => strtolower( $slug ),
'post_status' => 'publish',
'post_type' => strtolower( $post_type )
)
);
// If a template is specified in the function arguments, let's apply it
if( null != $template ) {
update_post_meta( get_the_ID(), '_wp_page_template', $template );
} // end if
return $page_id;
}
}
Ok so we created function which programatically register pages. It has 5 paramethers.
is Title
Slug
Post type
Shortcode.
Template
For the shortcode template you need to make a shortcode with the complete page output
and add it as a parameter to this function, so for registration page it will be a shortcode with the registration forms etc.
For example :
function registration_shortcode(){
echo 'Wellcome to Registration page';
}
add_shortcode('registration_output', 'registration_shortcode');
Next thing we need to call it once only when plugin loads.
so we do this :
function load_plugin() {
if ( is_admin() && get_option( 'Activated_Plugin' ) == 'Plugin-Slug' ) {
delete_option( 'Activated_Plugin' );
/* do stuff once right after activation */
// example: add_action( 'init', 'my_init_function' );
create_profile_page('Registration', 'registration', 'page', '[registration_output]');
create_profile_page('Profile', 'profile', 'page', '[profile_shortcode]');
create_profile_page('Profil Edit', 'profile-edit', 'page', '[edit_shortcode]');
}
}
add_action( 'admin_init', 'load_plugin' );
Ok so this will execute only once when plugin loads and it will create 3 Pages, which are Profile, Registration and Profile Edit.
And that's it, you have your front-end user profile blank pages, and you can write page output in shortcodes ,create more pages, put any forms or elements you like and create decent profile (which doesn't have any stuff you don't need in it like plugins. )
Hope this helps, it was painful for me to figure this out. Cheers!

Output buffering in wordpress

I'm trying to create a plugin for wordpress that automatically posts data from an API.
I've put the code that generates the HTML in a class:
class Poster{
public function generateHTML($data){
ob_start();
/*
some html and php code
*/
$html = ob_get_contents();
ob_end_flush();
return $html;
}
}
The event is triggered on the admin_menu action:
add_action('admin_menu', function(){
/*
get data from API
*/
$poster = new Poster();
$html = $poster->generateHTML($data);
$post = array(
'post_title' => $title,
'post_content' => $html,
'post_type' => 'post',
'comment_status' => 'open',
'ping_status' => 'open',
'post_status' => 'publish'
);
wp_insert_post($post, $wp_error, true);
});
Is there something wrong with my code?
It works sometimes but most of the time it doesn't.
And by 'it works' I mean the html is returned from the method and then stored in the $html variable. But most of the time the html returned from the method is the only one that's being outputted and it outputs in the admin panel(the rest of the admin panel is not outputted only the contents generated from the method is outputted).
So is there anything wrong with how I approach this?
Is there an alternative that I can do to achieve the same result? Thanks in advance!
Put a error_log message to find out the path and when it does display only your text in admin then catch it from there. Difficult to guess but you need debugging with error log.

Resources