I am encountering issue whereby my post content got duplicated. I want to be able to modify the content only on first publish. I tried to use wp_insert_post_data filter but it keeps crashing. So i use save_post. Here is what I want to achieve.
Example:
This is my post and I [bbsmall]create it using[/bbsmall] save_post
Filter:
Remove tag and use only simple html tags and do many other modification.
Ideal Output:
This is my post and I create it using save_post
However, my current output is:
This is my post and I create it using save_post
This is my post and I create it using save_post
This is my code
add_action('save_post', 'my_modified_content');
function my_modified_content($post_id) {
$post_content = get_post( $post_id );
if( $post_content->post_type != "post" ) {
return;
}else{
if (isset($post_content->post_status) && 'auto-draft' == $post_content->post_status ) {
return;
}
else{
$add_extra_tag = $myweb_settings['myoption_set'];
$post_checked = get_post_meta( $post_id, 'my_post_meta_style', true );
$alreadydone = get_post_meta( $post_id, 'check_post_style', true );
if ($alreadydone!=1){
if (isset($add_extra_tag) and $add_extra_tag=="Yes"){
remove_action('save_post', 'my_modified_content');
$content = add_simple_tags($post_content->post_content);
$title =add_simple_tags($post_content->post_title);
update_post_meta($post_id, 'check_post_style', '1');
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = $title;
$my_post['post_content'] = $content;
$my_post['post_status'] = 'publish';
wp_update_post($my_post);
add_action('save_post', 'my_modified_content');
}else{
if (isset($post_checked ) ) {
remove_action('save_post', 'my_modified_content');
$content = add_simple_tags($post_content->post_content);
$title =add_simple_tags($post_content->post_title);
update_post_meta($post_id, 'check_post_style', '1');
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = $title;
$my_post['post_content'] = $content;
$my_post['post_status'] = 'publish';
wp_update_post($my_post);
add_action('save_post', 'my_modified_content');
}else{
return ;
}
}
}else{
return ;
}
}
}
}
Okay so I am able to fix the issue.
Instead of using save_post, I use wp_insert_post_data filter.
Previously I failed to use the filter due to the wrong format of coding and I did not return the $data.
So these are the modifications I did. Hope this is helpful for others who are facing the same issue.
add_filter('wp_insert_post_data', 'my_modified_content',10,3);
function my_modified_content($data, $postarr, $unsanitized_postarr) {
if( $data['post_type'] != "post" ) {
return;
}else{
if (isset($data['post_status']) && 'auto-draft' == $data['post_status'] ) {
return;
}
else{
$add_extra_tag = $myweb_settings['myoption_set'];
$post_checked = $postarr['my_post_meta_style'];
//Do other checks and modifications
}
}
return $data;
}
Related
Some of my tags have some custom term meta and I'm saving that meta with this function:
add_action ( 'edit_term', 'save_termmeta_tag');
// save extra category extra fields callback function
function save_termmeta_tag( $term_id ) {
if ( isset( $_POST['Tag_meta'] ) ) {
$t_id = $term_id;
$tag_meta = get_option( "tag_$t_id");
$tag_keys = array_keys($_POST['Tag_meta']);
foreach ($tag_keys as $key){
if (isset($_POST['Tag_meta'][$key])){
$tag_meta[$key] = $_POST['Tag_meta'][$key];
}
}
//save the option array
update_option( "tag_$t_id", $tag_meta );
}
}
Now, I would like to get all tag with a specific option like 'channel'.
Is this possible, and how?
function get_term_with_option($opt) {
$terms = array();
// load all options
$options = wp_load_alloptions();
foreach($options as $k=>$v) {
// find keys with prefix `tag_`
if (strpos($k, 'tag_') === 0) {
$v = maybe_unserialize($v);
// check if has a meta key $opt
if (is_array($v) && array_key_exists($opt, $v)) {
$term = get_term(intval(str_replace('tag_', '', $k)));
if (!is_wp_error($term)) {
$terms[] = $term;
}
}
}
}
return $terms;
}
print_r(get_term_with_option('channel'));
I am wondering if there is any way to translate wordpress custom taxonomies on the frontend?
My idea is to intercept the rendering of taxonomies at some point (at my functions.php) and then render the translated strings which are stored in an array or JSON or txt file. I prefer not to use any plugin.
Your comments and and answers are welcome. Thanks.
add_filter('get_term', 'change_term_name_at_front', 10, 2);
function change_term_name_at_front($term, $taxonomy) {
//only for frontend
if( is_admin() ) {
return $term;
}
$new_names_array = array(
"taxonomy_id" => "New name",
"taxonomy_id2" => "New name2",
);
if ($taxonomy == 'your_taxonomy_name') {
$new_name = $new_names_array[$term->term_id];
if ($new_name !="") {
$term->name = $new_name;
}
}
return $term;
}
If you already have ACF plugin, you can add an additional field to the taxonomy and write there the new name, instead of using an array.
add_filter('get_term', 'change_term_name_at_front', 10, 2);
function change_term_name_at_front($term, $taxonomy) {
//only for frontend
if( is_admin() ) {
return $term;
}
if ($taxonomy == 'your_taxonomy_name') {
$key_field_acf = "yourtax_" . $term->term_id;
$new_name = get_field( 'new_name', $key_field_acf );
if ($new_name !="") {
$term->name = $new_name;
}
}
return $term;
}
I've created one of my themes.I want to add update functionality through api
The notice should be visible when I use the theme, if required for theme update
Actually i have no idea so please give me your suggestion or give me a code,so i can add functionality for update theme.
Note : If you have a no idea for api so you can provide another code
You can use site_transient_update_themes here:
add_filter ( 'site_transient_update_themes', 'theme_check_for_update' );
function theme_check_for_update ( $transient ) {
// Check Theme is active or not.
if( empty( $transient->checked['Your-Theme-Name'] ) )
return $transient;
$request = theme_fetch_data_of_latest_version();
if ( is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) != 200 ) {
return $transient;
} else {
$response = wp_remote_retrieve_body( $request );
}
$data = json_decode( $response );
if ( version_compare( $transient->checked['Your-Theme-Name'], $data->new_version, '<' ) ) {
$transient->response['Your-Theme-Name'] = (array) $data;
add_action('admin_notices', 'theme_update_admin_notice');
}
return $transient;
}
function theme_fetch_data_of_latest_version() {
// Your API call to check for new version
$request = wp_safe_remote_get( 'https://yourdomain.com/api/upgrade-json/' );
/*
Response Shoul be in following format:
{
"new_version": "1.0.4",
"url": "https://yourdomain.com/theme/changelog/",
"package": "https://yourdomain.com/theme/theme.zip"
}
*/
return $request;
}
function theme_update_admin_notice(){
echo '<div class="notice notice-warning notice-alt is-dismissible">
<p>New Theme Update is available.</p>
</div>';
}
Need to add blank and already exist validation for 'supports' => array( 'title') on my custom post type. But i dont want to use any plugin for this.
Thanks in advance.
add_action( 'admin_notices', 'custom_error_notice' );
function custom_error_notice(){
global $current_screen, $post;
if ( $current_screen->parent_base == 'edit' ){
if((!$post->post_name) && $_GET['post']) {
wp_redirect(admin_url('post-new.php?empty=1'));
}
if($_GET['empty']) echo '<div class="error"><p>Warning - Please fill up all fields correctly!</p></div>';
}
}
But this not working properly.
This may help you:-
add_action('save_post', 'album_save_post', 10, 2);
function album_save_post( $album_id, $album ) {
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || $album->post_type != 'music_album') return;
// echo '<pre>';
// print_r($album);
// echo '</pre>';
// die();
$errors = array();
// Validation filters
$title = $album->post_title;
if ( ! $title ) {
$errors['title'] = "The title is required";
}
// if we have errors lets setup some messages
if (! empty($errors)) {
// we must remove this action or it will loop for ever
remove_action('save_post', 'album_save_post');
// save the errors as option
update_option('album_errors', $errors);
// Change post from published to draft
$album->post_status = 'draft';
// update the post
wp_update_post( $album );
// we must add back this action
add_action('save_post', 'album_save_post');
// admin_notice is create by a $_GET['message'] with a number that wordpress uses to
// display the admin message so we will add a filter for replacing default admin message with a redirect
add_filter( 'redirect_post_location', 'album_post_redirect_filter' );
}
}
function album_post_redirect_filter( $location ) {
// remove $_GET['message']
$location = remove_query_arg( 'message', $location );
// add our new query sting
$location = add_query_arg( 'album', 'error', $location );
// return the location query string
return $location;
}
// Add new admin message
add_action( 'admin_notices', 'album_post_error_admin_message' );
function album_post_error_admin_message() {
if ( isset( $_GET['album'] ) && $_GET['album'] == 'error' ) {
// lets get the errors from the option album_errors
$errors = get_option('album_errors');
// now delete the option album errors
delete_option('album_errors');
$display = '<div id="notice" class="error"><ul>';
// Because we are storing as an array we should loop through them
foreach ( $errors as $error ) {
$display .= '<li>' . $error . '</li>';
}
$display .= '</ul></div>';
// finally echo out our display
echo $display;
// add some jQuery
?>
<script>
jQuery(function($) {
$("#title").css({"border": "1px solid red"})
});
</script>
<?php
}
}
i got the solution.
/** ADD Validation for title */
function force_post_title_init()
{
wp_enqueue_script('jquery');
}
function force_post_title()
{
echo "<script type='text/javascript'>\n";
echo "
jQuery('#publish').click(function(){
var testervar = jQuery('[id^=\"titlediv\"]')
.find('#title');
if (testervar.val().length < 1)
{
jQuery('[id^=\"titlediv\"]').css('border', '1px solid red');
alert('Post title is required');
return false;
}
});
";
echo "</script>\n";
}
add_action('admin_init', 'force_post_title_init');
add_action('edit_form_advanced', 'force_post_title');
// Add this row below to get the same functionality for page creations.
add_action('edit_page_form', 'force_post_title');
May be this also help for all of you.
I need to abort the post saving process when the post content contains a specific string and then display a message to the user.
I found a method to display the message but didn't find a way to refuse post saving.
So far here's what i've done
add_action( "pre_post_update", "checkPost");
function checkPost($post_ID) {
$post = get_post($post_ID);
$postContent = $post->post_content;
if ( wp_is_post_revision( $post_ID ) )
return;
if(preg_match("/bad string/", $postContent) == 1) {
//
// cancel post save
//
// then
add_filter("redirect_post_location", "my_redirect_post_location_filter", 99);
}
}
function my_redirect_post_location_filter($location) {
remove_filter('redirect_post_location', __FUNCTION__, 99);
$location = add_query_arg('message', 99, $location);
return $location;
}
add_filter('post_updated_messages', 'my_post_updated_messages_filter');
function my_post_updated_messages_filter($messages) {
$messages['post'][99] = 'Publish not allowed';
return $messages;
}
Hope this helps you to check post content
function to_err_is_human( $post_id ) {
// If this is just a revision, don't check
if ( wp_is_post_revision( $post_id ) )
return;
$post_content = wp_unslash(!empty($_REQUEST['content']) ? $_REQUEST['content'] : $post_data['content']);
if ($post_content=="abc")
{ add_filter("redirect_post_location", "my_redirect_post_location_filter", 99);}
}
add_action( 'save_post', 'to_err_is_human' );
function my_redirect_post_location_filter($location) {
remove_filter('redirect_post_location', __FUNCTION__, 99);
$location = add_query_arg('message', 99, $location);
return $location;
}
add_filter('post_updated_messages', 'my_post_updated_messages_filter');
function my_post_updated_messages_filter($messages) {
$messages['post'][99] = 'Publish not allowed';
return $messages;
}