I've read and tried a lot before posting this.
I have a custom sitemap function that I want to trigger on update/create/save post.
I've tried to create new post, update an existing one and just save for the following hooks:
save_post
acf/save_post
pre_post_update
edit_post
the_post
publish_post
Nothing of the above works. I unit tested the function and it does work when hooked on init and admin_init. So I know it's a hook problem. Any advice would be highly appreciated.
All the code:
function pull_active_pages(){
global $wpdb;
$query = $wpdb->get_results(" SELECT * FROM `wp_posts` WHERE (`post_type` LIKE 'page' OR `post_type` LIKE 'post') AND `post_status` LIKE 'publish'; ", OBJECT);
return $query;
}
function makeSitemap(){
$file = 'sitemap.xml'; //that means in the root, provide full path
$query = pull_active_pages();
$output = '<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL;
$output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-news/0.9 http://www.google.com/schemas/sitemap-news/0.9/sitemap-news.xsd">'.PHP_EOL;
foreach((array)$query as $record){
$output .= '<url>'.PHP_EOL;
$url = get_permalink($record->ID);
$output .= '<loc>' . $url . '</loc>'.PHP_EOL;
$last_mod = $record->post_modified_gmt;
$output .= '<lastmod>' . $last_mod .'</lastmod>'.PHP_EOL;
$output .= '<priority>1.00</priority>'.PHP_EOL;
$output .= '</url>'.PHP_EOL;
}
$flags = FILE_APPEND; //redo the whole thing no append for now
$output .='</urlset>';
file_put_contents( $file, $output);
}
add_action('save_post', function(){ makeSitemap();}, 10, 2);
You should change the action like that
add_action('save_post', 'makeSitemap');
and add absolute path to the filename
$file = ABSPATH . '/sitemap.xml'; //that means in the root, provide full path
Check this https://wpengineer.com/2382/wordpress-constants-overview/#ABSPATH
for other absolute paths constants
Related
I'm attempting to add a feature to a plugin that extends media management. This feature would allow you to rename an attachment file. I've been able to complete this with the following code.
public function update_attachment_filename( $post_ID ) {
// Get path to existing file
$file = get_attached_file( $post_ID );
$path = pathinfo( $file );
// Generate new file name
$file_updated = $path['dirname'] . '/' . $_POST['update_filename'] . '.' . $path['extension'];
// Update the name and reference to file
rename( $file, $file_updated );
update_attached_file( $post_ID, $file_updated );
}
While the original file gets renamed using the above method, all additional image sizes defined in the plugins/theme are not updated. I'm struggling to figure out the best way to accomplish this task.
I've looked into wp_update_attachment_metadata() and wp_generate_attachment_metadata() but am unsure whether they will give me the desired functionality.
Additionally I've looked into something such as:
$file_meta = wp_get_attachment_metadata( $post_ID );
foreach( $file_meta['sizes'] as $image ) {
// Do something
}
Any nudge in the right direction would be greatly appreciated.
Thanks!
I was able to utilize both the wp_generate_attachment_metadata() and the wp_update_attachment_metadata() function to achieve the desired end result.
public function update_attachment_filename( $post_ID ) {
if( isset( $_POST['update_filename'] ) && ! empty( $_POST['update_filename'] ) ) {
// Get path to existing attachment
$file = get_attached_file( $post_ID );
$path = pathinfo( $file );
// Create new attachment name
$file_updated = $path['dirname'] . '/' . $_POST['update_filename'] . '.' . $path['extension'];
// Update the attachment name
rename( $file, $file_updated );
update_attached_file( $post_ID, $file_updated );
// Update attachment meta data
$file_updated_meta = wp_generate_attachment_metadata( $post_ID, $file_updated );
wp_update_attachment_metadata( $post_ID, $file_updated_meta );
}
}
I have some blog. In one of my category i have 500 posts. I need to insert one simply link on the top of all of this posts which are for example cat=5.
How can i do it using mysql?
Thanks in advance!
you don't need to do it in mysql, you can use a hook.
Something like that:
function append_content($content) {
global $post;
if (in_category( 5, $post )) {
$original = $content;
$content = "<div class=\"ikaz\">";
$content .= "hello all";
$content .= "</div>";
$content .= $original;
return $content;
}
}
add_filter( 'append_content', 'like_content' );
I am using Tim Whitlock's Latest Tweets plugin to display tweets.
I would like to modify the plugin's default output. I have found the below in line 137 of the plugin file:
$final = apply_filters('latest_tweets_render_tweet', $html, $date, $link, $tweet );
if( $final === $html ){
$final = '<p class="tweet-text">'.$html.'</p>'.
'<p class="tweet-details">'.$date.'</p>';
}
$rendered[] = $final;
And have come up with this filter to attempt to change the output:
add_filter('latest_tweets_render_tweet', 'modified_tweets', 10);
function modified_tweets( $html, $date, $link, $tweet ) {
$final = '<div class="col-sm-4">'.
'<p class="tweet-text2">'.$html.'</p>'.
'<p class="tweet-details">'.$date.'</p>'.
'</div>';
$rendered[] = $final;
};
However, the output remains the same. Any ideas what I'm doing wrong?
You have to return that array, so add:-
return $rendered;
after this:-
$rendered[] = $final;
As DWX mentioned, you're missing the return. But you don't need to return an array, the original code is expecting a string:
$my_final = '<div class="col-sm-4">'. $etcetera;
return $my_final;
And also add the number of arguments after the priority when adding the filter:
add_filter('latest_tweets_render_tweet', 'modified_tweets', 10, 4);
I need to know if it's possible how to get_the_tags() to array?
i want to like this
$myarray = array('one', 'two', 'three', 'four', 'five', 'six');
i want use this code with "replace the_content" like this
<?php
function replace_content($content){
foreach(get_the_tags() as $tag) {
$out .= $tag->name .',';
$csv_tags .= '"' . $tag->name . '"';
}
$find = array($out);
$replace = array($csv_tags);
$content = str_replace($find, $replace, $content);
return $content;
}
add_filter('the_content', 'replace_content');
?>
find tag in content and replace with link
$posttags = get_the_tags();
$my_array = array();
if ($posttags) {
foreach($posttags as $tag) {
$my_array[] = $tag->name ;
}
.. and if your final goal is to output it like you wrote above then :
echo implode(',', $my_array);
.. and by the type of question , I was not sure if by one,two.. you might be meaning ID , so :
$posttags = get_the_tags();
$my_array = array();
if ($posttags) {
foreach($posttags as $tag) {
$my_array[] = $tag->term_id ;
}
BTW - a quick look at the codex would have shown you that ...
You should be able to do something like this:
global $wpdb;
// get all term names in an indexed array
$array = $wpdb->get_results("SELECT name FROM wp_terms", ARRAY_N);
// walk over the array, use a anonymous function as callback
array_walk($array, function(&$item, $key) { $item = "'".$item[0]."'"; });
Notice that anonymous functions are only available since PHP 5.3
You should be able to do the same thing using get_the_tags() if you only want tags for a specific post:
$tags = get_the_tags();
array_walk($tags, function(&$item, $key) { $item = "'".$item->name."'"; });
Judging from your updated question you don't need any of the above code, the only thing you need to to in order to get single quotes around each tag is:
foreach(get_the_tags() as $tag) {
$out .= $tag->name . ',';
$csv_tags .= '"' . "'".$tag->name."'" . '"';
}
I have function which count page views in Wordrpess, but when I reload page add_action() call function 5 times, I not know why.
<?php
add_action('wp', 'wp125_adview');
function wp125_adview(){
global $wpdb;
$adtable_name = $wpdb->prefix . "wp125_ads";
$url = $_SERVER['REQUEST_URI'];
if(isset($_POST['unique_hidden_field'])) {
$update = "UPDATE ". $adtable_name ." SET views = views+1 WHERE status!=0";
$results = $wpdb->query( $update );
}
}
?>
You have hooked your function call into the 'wp' action hook.
It might be better to use something that should run only once per page, e.g.
add_action('wp_footer', 'wp125_adview');