Wordpress wp_insert_post function - wordpress

I am writing a php program to insert a wordpress post using wordpress functions. When I use the function wp_insert_post(), any <script> tags in my post content are removed. Is there is a way to override this in Wordpress?

The solution by pp19dd doesn't work anymore in newer versions of WordPress. The new way, as suggested by a WordPress developer (but not recommended, because of security issues):
filter had to be unset due to security problems with it. You could try doing kses_remove_filters() before inserting the post and kses_init_filters() after inserting the post if you are trying to avoid the kses filtering of the post fields. Just be wary since that defeats most of the security measures for inserting posts.
So:
kses_remove_filters();
wp_insert_post( $postdata );
kses_init_filters();
Source: https://wordpress.org/support/topic/bypass-sanitize_post-from-wp_insert_post

Related

Can I remove the JSON-LD schema that Yoast adds to my WordPress site?

I would like to remove the JSON-LD schema that Yoast applies to my WordPress site so that I can add my own. I have already added my own, and Google Structured Data Testing says that it is OK, but basically I have 3 separate JSON-LD schemas instead of two because of Yoast.
You can see what I mean here: https://search.google.com/structured-data/testing-tool/u/0/#url=http%3A%2F%2Fwww.yogabearpc.com
Yoast has added the WebSite schema and it seems unnecessary or even damaging?
I wanted to disable this because of the sitelinks searchbox and the fact that I don't have a search function that works globally, just on the blog. Having the search box enabled for me would have undesirable effects.
The easier option may just be to prevent Google using the sitelinks searchbox without having to touch the functions files. You can prevent Google using sitelinks searchbox on your site by using the following meta:
<meta name="google" content="nositelinkssearchbox" />
If you want to disable Yoast's JSON-LD all together then here's a snippet from my blog and the code I use on my site:
SOURCE
How to disable Yoast SEO Schema JSON-LD completely
function bybe_remove_yoast_json($data){
$data = array();
return $data;
}
add_filter('wpseo_json_ld_output', 'bybe_remove_yoast_json', 10, 1);
Login to your WordPress dashboard and head over to the editor
within the tab menu appearance, find your functions file (normally
named functions.php) and add the code below just before the PHP tag is
closed at the bottom.
Simplest way to completely disable the Yoast SEO schema JSON-LD
Add this line to functions.php file:
add_filter( 'wpseo_json_ld_output', '__return_empty_array' );
Source
If you want to disable just Organization or just Website, add this to your theme's functions.php file:
function bybe_remove_yoast_json($data){
if ( (isset($data['#type'])) && ($data['#type'] == 'Organization') ) {
$data = array();
}
return $data;
}
add_filter('wpseo_json_ld_output', 'bybe_remove_yoast_json', 10, 1);
Unless the data Yoast produces is wrong, there is no harm in having it. Quite the contrary, having more structured data is better than having less.
If having it is "unnecessary" depends on your definition of what is necessary. Some consumers might be interested in it, others not.
My guess is that Yoast adds a WebSite entity because of Google’s sitelinks searchbox rich snippet result, which allows Google users to search your site directly from the Google search result.

Find from where the data comes to "the_content()" function

This might look an ordinary question though I'm stuck in it. I'm new to the wordpress. I've bought a wordpress theme and I'm trying to edit some pages as I want. Now I want to edit the default post page where I've already started editing the "single.php" and "post-format.php" files. I want to know from where or how "the_content()" function gets data?
Since I want some html part of the page to be removed though that html part comes through a "the_content" function. Therefore, I'm unable to remove that part without getting rid of "the_content" function. But I can't get rid of "the_content" function because the very same function calls some important part too.
Hope you guys can help!
From Where
As a function get_the_content() retrieve the post content (Generally Used in a Loop) from database and prints on the frontend.
And again the_content() as filter controls how you show the post content.
How
Dead simple answer by WordPress API. You need to understand WordPress Database_API for in-depth understanding.
Frequently Used by
wp-includes/plugin.php: apply_filters()
wp-includes/post-template.php: get_the_content()
wp-includes/post-template.php: the_content
Usage Case: (from Codex)
post_password_required()
get_the_password_form() if post_password_required() fails
wp_kses_no_null() while processing the tag.
balanceTags()
get_permalink()
global: $id, $post ,$more, $page, $pagesm, $multipage, $preview, $pagenow
Ref:
- Filter the_content() | Functionthe_content()
Optional: Offline WP Codex Docs Search/Browser for Windows/OSx

Completely custom page in WordPress generated by a plugin

I am writing a WordPress plugin that has an AJAX element to it - blocks of HTML are fetched by the front end from the plugin using AJAX.
I am having difficulty joining up the pieces here, and I suspect it is just a terminology issue. How would I implement a page completely provided by the plugin?
The content of the page will be HTML - the plugin can generate this in response to POST or GET parameters.
There needs to be a route to this page. The route does not have to be user-friendly or a REST style - just some kind of URI that gets to the plugin. Is there perhaps a way to register a custom page with an arbitrary name, without having to create it as a WP post?
I would like all this to be self-contained in the plugin, so should not involve the administrator having to create posts or pages, or have to add anything to the theme.
Ideally I would avoid any URLs that go into the wp-admin directory. End users don't belong in here.
I would strongly suggest referring to https://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side
You need to have a php script in your plugin directory that returns what you request, and you need to determine that url at run time for reference in your ajax. The above link gives an example for enqueuing and using wp_localize_script() to provide the url for your custom php script.
wp_enqueue_script( 'ajax-script',
plugins_url( '/js/my_query.js', __FILE__ ), array('jquery') );
// in JavaScript, object properties are accessed as
// ajax_object.ajax_url, ajax_object.we_value
wp_localize_script( 'ajax-script', 'ajax_object',
array( 'ajax_url' => plugins_url( '/php/myapi.php' ));
Your javascript file will be included on every page and will listen for events on the page which require a block of HTML, as you've described it.
Your file myapi.php then needs to take a request, probably using a query string, get the appropriate content from the wordpress api, and respond with said content, which your javascript will put into place.
To have access to the wordpress api as well though, you have two options:
Force wordpress to run starting with your file, by including wp-load.php. This is probably not the cleanest way.
Set up a custom page or slug to direct to your plugin.
I would advise the second option, and advise a slug, in which case you may find this post helpful: wp_rewrite in a WordPress Plugin
From Jason's comment, based on the link above:
The rewrite rules are mentioned a lot, but are really a distraction -
they just help to make URLs look more "friendly", which was not an
objective here. The key is: register a custom GET parameter; look for
that parameter early in the page rendering process; if you find the
parameter is set, then output/echo stuff and die(). There are a
number of hooks that can be used to look at the parameters, chosen
dependin on how much you want WP to set up and process first.

Wordpress query_posts to modify wp_query

When I alter main $wp_query with query_posts function, all my conditional tags are false and all pages have [is_home] => 1.
So all custom templates I made by modifying main query to save some time are now home. Anyone knows a fix for this?
You should never use query_posts to create custom queries. query_posts breaks the main query, as you have seen. Whether or not to use a custom query for your specific needs, I don't know.
I have done a complete post about this subject on WPSE that you can go and check here. Also go and check out Theme Development in the codex

Custom meta for custom post type not importing / exporting

When trying to export and then import using the wordpress built in export and the wordpress import plugin I have noticed custom meta for custom post types isn't being imported.
This is a real problem for me as i've spent alot of time working on a local version of site and I could do with the custom meta carrying over.
Has anyone else experienced this problem before as well?
From what I can tell doing my own tests, this happens because WordPress is creating existing custom fields (post meta) automatically when importing. Thus, you'll have duplicate values for the same field and WP will load the first one it sees, which might be the blank automatically created value. Because of the network installation I work with, I could only try this with version 3.1.2. It might be fixed in newer releases.
To fix it, you can put in the following code in your functions.php to remove post meta with a blank value for your custom keys.
function my_init() {
global $wpdb;
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE (`meta_key` = 'some_key' OR `meta_key` = 'another_key') AND `meta_value` = ''");
}
add_action('init', 'my_init');
once your import is done, you can disable or remove this code.
Strange...However you can try exporting your database.
Try This addon for wordpress ctp plugin.
https://wordpress.org/plugins/exportimport-custom-post-and-taxonomy-type/

Resources