How to update wordpress content - wordpress

add_filter('the_content','filter_trendland_content');
//create a the function to get the content of the page by using the hook the_content
function filter_trendland_content($content) {
$getdata = $content;
if ( preg_match_all( '#(?:<a.+?href=["|\'](?P<link_url>.+?)["|\'].*?>\s*)?(?P<img_tag><img.+?src=["|\'](?P<img_url>.+?)["|\'].*?>){1}(?:\s*</a>)?#is', $getdata, $images ) ) {
foreach ( $images[img_url] as $key => $value) {
$subdomain = rand( 1, 3 );
$newurl="yaseen.media$subdomain.com";
$content = preg_replace('/\bmedia.trendland.com\b/', $newurl, $value);
print $content;
}
print $getdata;
}
//return $getdata;
}
I am using the above method to replace the url's with the new one and update the content of the page but the page content is still the same so please help me with this..

Looking at your function, it does not return a value.
Instead of printing the output, return the content back to the filter.
Also, you are iterating through an empty object - $images[img_url]. Check the documentation for preg_match_all. It returns a multi-dimensional array of matches.
You code looks over-engineered and the word boundary in your regex would probably prevent a match. The following may achieve what you need providing that all instances of media.trendland.com require replacing.
function filter_trendland_content($content) {
$subdomain = rand( 1, 3 );
$newurl='yaseen.media' . $subdomain . '.com';
$content = preg_replace('/media.trendland.com/', $newurl, $content);
return $content;
}

Related

Wordpress - setting a custom post per category

I have added the below code to the functions.php file to load different post templates per category. The code is doing what it is supposed to do. The issue I am facing is that when I open posts these load correctly but whilst loading an error is displayed for a instant saying "trying to get property of non-object in .......functions.php" How may I get rid of that error? Appreciate your help.
/* ----------------------------------------------------------------------------
custom single posts per category
*/
add_filter('single_template', 'check_for_category_single_template');
function check_for_category_single_template( $t )
{
foreach( (array) get_the_category() as $cat )
{
if ( file_exists(get_stylesheet_directory() . "/single-category-{$cat->slug}.php") ) return get_stylesheet_directory() . "/single-category-{$cat->slug}.php";
if($cat->parent)
{
$cat = get_the_category_by_ID( $cat->parent );
if ( file_exists(get_stylesheet_directory() . "/single-category-{$cat->slug}.php") ) return get_stylesheet_directory() . "/single-category-{$cat->slug}.php";
}
}
return $t;
}
The error trying to get property of non-object means, you try to get a property of something that is not an object. So looking at your code you are getting two properties with ->: "slug" and "parent".
This gives us the hint, that the value saved in the $cat variable might not be an object.
You are using get_the_category_by_ID which gives you a name, not a category object. https://developer.wordpress.org/reference/functions/get_the_category_by_id/ So you cannot access any property. The problem is this: get_the_category_by_ID( $cat->parent ); There is no parent property in $cat, because it only contains the name. You need to get the object to make use of the parent property. Moreover the property is called category_parent, and not parent.
$parent_id = $cat->category_parent;
$cat = get_category($parent_id);
This should solve your problem.
If it does not solve your problem, I wrote the code in another way. It shows single steps and maybe it is useful for you or some other users.
The function to get all your category objects is: get_categories() https://developer.wordpress.org/reference/functions/get_categories/
You can save all the IDs of the categoires into a array. After that, you loop through each position of the array and check if the single post you are viewing has the same category ID. We can check if the post is in given categories using in_category() https://developer.wordpress.org/reference/functions/in_category/
I did no testing, but the code can look something like:
add_filter('single_template', 'check_for_category_single_template');
function check_for_category_single_template() {
// Get all category objects
$categories = get_categories();
// Array for IDs
$categories_ids = [];
foreach($categories as $category) {
// add id to array
array_push($categories_ids, $category->cat_ID);
}
// Loop through array of IDs
foreach($categories_ids as $category_id) {
// Check if ID equals current post category ID
if ( in_category($category_id) ) {
// get current category object
$cat = get_category($category_id);
// check if category has parent
if( $cat->category_parent > 0 ) {
$parent_id = $cat->category_parent;
// get parent object
$cat = get_category( $parent_id );
}
// get slug of category
$cat_slug = $cat->slug;
if ( file_exists(get_stylesheet_directory() . "/single-category-" .$cat_slug. ".php") ) {
return get_stylesheet_directory() . "/single-category-" .$cat_slug. ".php";
}
}
}
}

Woocommerce add img tag on order admin details page

I have a wordpress website where customers make an image with text and icons, once processed thru woocommerce and payed for that image name 12345.png is saved as Customer_product_image
function add_order_item_meta($item_id, $values) {
$key = 'customer_product_image'; // Define your key here
$value = $values['user_img']; // Get your value here
woocommerce_add_order_item_meta($item_id, $key, $value);
}
And i works great, but now i'm banning my head against the wall! When the purchased image is displayed on the Order admin detail page, it shows up as CUSTOMER_PRODUCT_IMAGE: 1234.png how on earth would i go about wrapping that within an image tag so the image is displayed there?
I've searched high and low on google but haven't been able to find anything, its probably that i dont know what do actually search for....
This did the trick for me!
First i added this snippet for removing the custom meta item on order detail render:
add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_order_item_meta_fields' );
function hide_order_item_meta_fields( $fields ) {
$fields[] = 'current_view';
$fields[] = 'custom_image';//Add all meta keys to this array,so that it will not be displayed in order meta box
return $fields;
}
second i added it back with this, and with the desired text and image tag:
add_action( 'woocommerce_after_order_itemmeta', 'order_meta_customized_display',10, 3 );
function order_meta_customized_display( $item_id, $item, $product ){
$all_meta_data=get_metadata( 'order_item', $item_id, "", "");
$useless = array(
"_qty","_tax_class","_variation_id","_product_id","_line_subtotal","_line_total","_line_subtotal_tax","_line_tax","_line_tax_data"
);// Add key values that you want to ignore
$customized_array= array();
foreach($all_meta_data as $data_meta_key => $value) {
if(!in_array($data_meta_key,$useless)){
$newKey = ucwords(str_replace('_'," ",$data_meta_key ));//To remove underscrore and capitalize
$customized_array[$newKey]=ucwords(str_replace('_'," ",$value[0])); // Pushing each value to the new array
}
}
if (!empty($customized_array)) {
foreach($customized_array as $data_meta_key => $value){
echo "<div class='product_container'><span>Produkt Billede: </span><img src='".wp_upload_dir()['baseurl'].'/flapper/'. $value ."' /> </div>";
}
}
}
i found the answer to this question on this page
You can use the filter woocommerce_order_item_display_meta_value to output the image. Place this code in your functions.php file, you'll need to modify the src attribute of the img tag to include the appropriate URL before the filename value. You can also modify the display label with the filter woocommerce_order_item_display_meta_key
add_filter( 'woocommerce_order_item_display_meta_value', 'modify_order_item_display_value' , 10, 3 );
function modify_order_item_display_value( $display_value, $meta, $wc_order_item ) {
$meta_data = $meta->get_data();
if( $meta_data['key'] === 'customer_product_image' ) {
return '<img src="' . $meta_data['value'] . '">';
}
return $display_value;
}
add_filter('woocommerce_order_item_display_meta_key', 'modify_order_item_display_key', 10, 3);
function modify_order_item_display_key( $display_key, $meta, $wc_order_item ) {
$meta_data = $meta->get_data();
if( $meta_data['key'] === 'customer_product_image' ) {
return 'Customer Image';
}
return $display_key;
}

get_post_meta() returns empty

I'm doing a basic shortcode for translating a text in the footer of a wordpress site, and I'm using the get_post_meta() for identifing the language in a meta tag (which I'm adding with a plug in for inserting html code in the header) but it returns empty I'm wondering if this is because the plugin and the order in which he creates the elements(first he executes my shortcode and then the plugin) or if it is something else.
function text_Footer($atts, $content=null){
extract(shortcode_atts(array(
'id' => ''
), $atts));
$ID = get_the_ID();
$lang = get_post_meta(ID,'language',true);
if($lang == 'portuguese')
{
$output='<p>Text in portuguese</p>';
}
else
{
//echo $lang."nope";
$output = '<p>Text in spanish</p>';
}
return $output;
}
get_the_ID() function must be within The Loop.
if you want to extract id from shortcode, just need to use it in write way: $lang = get_post_meta($id,'language',true);

FeedWordPress - Save string as Custom Field

I am using the FeedWordPress plugin http://wordpress.org/extend/plugins/feedwordpress/ to pull posts from one site to another.
I have written a filter that after some help from Stack users successfully scans the $content and extracts the image URL into $new_content
define('FWPASTOPC_AUTHOR_NAME', 'radgeek');
add_filter(
/*hook=*/ 'syndicated_item_content',
/*function=*/ 'fwp_add_source_to_content',
/*order=*/ 10,
/*arguments=*/ 2
);
function fwp_add_source_to_content ($content, $post) {
// Use SyndicatedPost::author() to get author
// data in a convenient array
$content = $post->content();
// Authored by someone else
if( preg_match( '/<img[^>]+src\s*=\s*["\']?([^"\' ]+)[^>]*>/', $content, $matches ) ) {
$new_content .= 'URL IS '.$matches[0].'';
return $new_content;
}
else
{
}
}
What I wanted to do now was save this URL into a custom field instead of just returning it. Has anyone achieved anything similar?
So as I understand it, the plugin grabs content from external RSS feeds and creates them as posts in your website.
If this is the case, using your filter you should be able to grab the post ID within the $post variable.
So all you need is the add_post_meta() function to add a custom field to the specific post.
So including your code above it should look something like:
define('FWPASTOPC_AUTHOR_NAME', 'radgeek');
add_filter(
/*hook=*/ 'syndicated_item_content',
/*function=*/ 'fwp_add_source_to_content',
/*order=*/ 10,
/*arguments=*/ 2
);
function fwp_add_source_to_content ($content, $post) {
// Use SyndicatedPost::author() to get author
// data in a convenient array
$content = $post->content();
// Authored by someone else
if( preg_match( '/<img[^>]+src\s*=\s*["\']?([^"\' ]+)[^>]*>/', $content, $matches ) ) {
$new_content .= 'URL IS '.$matches[0].'';
//Add custom field with author info to post
add_post_meta($post->ID, 'post_author', $new_content);
return $new_content;
}
}

Wordpress: is child of category function

I I am looking for a function that merely returns true or false. The function would check if the category archive being queried is a child of a particular parent category. The function would look something like this (similar to the is_category() function):
if(is_child_of_category('3')){ //do something }
if(is_child_of_category('4')){ //do something else }
if(is_child_of_category('5')){ //do something entirely different }
I have looked through the documentation and the forums but have not found any solutions, any ideas if this has been done or how one would go about it?
/////////////
addendum
////////////
Here is the working code that I wrote with help from Gavin:
Thanks Gavin
first a function for the functions.php file to get the category id:
function getCurrentCatID(){
global $wp_query;
if(is_category() || is_single()){
$cat_ID = get_query_var('cat');
}
return $cat_ID;
}
Next use cat_is_ancestor_of
$post = $wp_query->post;
$id = getCurrentCatID();
if ( cat_is_ancestor_of(3, $id) { include(TEMPLATEPATH . '/unique.php'); }
cat_is_ancestor_of
This will return true still, however, if the category is a grandchild of the specified category.
I need something similar yesterday, so I wrote this function which hands me back an array with all the child categories + the parent-category (which might be useful if you want to, let's say, hide the comment form on all FAQ-cat posts and childs thereof, but show it on others).
Pass $catname as a var and get an array back, which you can use as an argument in e.g. in_category() function.
// Get List of Child Categories
function get_child_cats( $catname ) {
$parentcat = get_cat_ID( '$catname' );
$subcat = get_categories( array('child_of' => $parentcat ) );
$cat_array = array();
array_push($cat_array, $parentcat); // add the parent cat to the array
foreach ($subcat as $sc) {
array_push($cat_array, $sc->cat_ID);
}
return $cat_array;
}

Resources