How to get WordPress theme name by custom URL - wordpress

I'm trying to get WordPress theme name using API. I have tried this code but I want the same result using custom url:
function theme_list_function(){
// Get a list of themes
$list = wp_get_themes();
// Return the value
var_dump($list);
}
This code will return current WordPress theme list. I want theme list form a URL like xyz.com

I have achieved this using this code:
$target_site = ""; // put your wordpress url here
$src = file_get_contents( $target_site );
preg_match("/\<link rel='stylesheet'.*href='(.*?style\.css.*?)'.*\>/i", $src, $matches );
if( $matches ) {
$style_href = trim( $matches[1] );
$style_src = file_get_contents( $style_href );
preg_match( "/\Theme Name:(.*?)\n/i", $style_src, $theme_name );
$theme_name = str_replace(' ', '', $theme_name[1]);
var_dump( $theme_name );
}

Related

Woocommerce order_review custom template on cart items update

I have develop a custom template for woocommerce checkout page and added all templates that I need to change specifically order_review.php in plugin/woocommerce/checkout/order_review.php and on order page it works perfect.
From Order page I can remove some products or add products through ajax and here is my ajax code.
ob_start();
woocommerce_order_review();
$woocommerce_order_review = ob_get_clean();
$response = array(
'cart_total' => WC()->cart->total,
'cart_item_key' => $new_key,
'fragments' => apply_filters(
'woocommerce_update_order_review_fragments',
array(
'.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
)
),
);
if ( ! empty( $data ) ) {
$response['cartflows_data'] = $data;
}
return $response;
And the woocommerce_order_review(); loads woocommerce default template instead of template from my plugin.
WooCommerce does not look in plugin folders for templates by default. You need to use the filter woocommerce_locate_template in your plugin to tell WooCommerce to look in your plugin folder for templates. The solution is outlined by SkyVerge in this blog post: https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/
I am including the solution here in case the post is every removed for some reason:
function myplugin_plugin_path() {
// gets the absolute path to this plugin directory
return untrailingslashit( plugin_dir_path( __FILE__ ) );
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
$_template = $template;
if ( ! $template_path ) $template_path = $woocommerce->template_url;
$plugin_path = myplugin_plugin_path() . '/woocommerce/';
// Look within passed path within the theme - this is priority
$template = locate_template(
array(
$template_path . $template_name,
$template_name
)
);
// Modification: Get the template from this plugin, if it exists
if ( ! $template && file_exists( $plugin_path . $template_name ) )
$template = $plugin_path . $template_name;
// Use default template
if ( ! $template )
$template = $_template;
// Return what we found
return $template;
}
You have to override WooCommerce Template
woocommerce/templates/checkout/review-order.php and Put this file in your theme folder themes/yourthemename/woocommerce/checkout/review-order.php

Wordpress remove shortcode and save for use elsewhere

Trying to remove the gallery shortcode from the post content and save in a variable for use elsewhere in the template. The new Wordpress gallery tool is great for selecting which images they want and assigning captions, hoping to use this to create the gallery, but then pull it out of the content on the front-end.
So this little snipped works just fine for removing the gallery and reapplying formatting... however I want to save that gallery shortcode.
$content = strip_shortcodes( get_the_content() );
$content = apply_filters('the_content', $content);
echo $content;
Hoping to save the shortcode so it can be parsed into an array and used to recreate a custom gallery setup on the front-end. An example of this shortcode I'm trying to save is...
[gallery ids="1079,1073,1074,1075,1078"]
Any suggestions would be greatly appreciated.
Function to grab First Gallery shortcode from post content:
// Return first gallery shortcode
function get_shortcode_gallery ( $post = 0 ) {
if ( $post = get_post($post) ) {
$post_gallery = get_post_gallery($post, false);
if ( ! empty($post_gallery) ) {
$shortcode = "[gallery";
foreach ( $post_gallery as $att => $val ) {
if ( $att !== 'src') {
if ( $att === 'size') $val = "full"; // Set custom attribute value
$shortcode .= " ". $att .'="'. $val .'"'; // Add attribute name and value ( attribute="value")
}
}
$shortcode .= "]";
return $shortcode;
}
}
}
// Example of how to use:
echo do_shortcode( get_shortcode_gallery() );
Function to delete First gallery shortcode from Post content:
// Deletes first gallery shortcode and returns content
function strip_shortcode_gallery( $content ) {
preg_match_all( '/'. get_shortcode_regex() .'/s', $content, $matches, PREG_SET_ORDER );
if ( ! empty( $matches ) ) {
foreach ( $matches as $shortcode ) {
if ( 'gallery' === $shortcode[2] ) {
$pos = strpos( $content, $shortcode[0] );
if ($pos !== false)
return substr_replace( $content, '', $pos, strlen($shortcode[0]) );
}
}
}
return $content;
}
// Example of how to use:
$content = strip_shortcode_gallery( get_the_content() ); // Delete first gallery shortcode from post content
$content = str_replace( ']]>', ']]>', apply_filters( 'the_content', $content ) ); // Apply filter to achieve the same output that the_content() returns
echo $content;
just use the get_shortcode_regex():
<?php
$pattern = get_shortcode_regex();
preg_match_all('/'.$pattern.'/s', $post->post_content, $shortcodes);
?>
that will return an array of all the shortcodes in your content, which you can then output wherever you feel, like so:
<?php
echo do_shortcode($shortcodes[0][1]);
?>
similarly, you could use the array entries to check for shortcodes in your content and remove them with str_replace():
<?php
$content = $post->post_content;
$content = str_replace($shortcodes[0][1],'',$content);
?>
Something like $gallery = do_shortcode('[gallery]'); might work.

How to Fetch the Shortcode Parameters Value in WordPress?

[amazon_s3 bucket=my_bucket_name object=my_file_name.ext]
Hi all i need to know how to fetch the parameter value in the above shortcode. For Example: Object is the attributes then how can i fetch the object value my_file_name.ext. I am using the woocommerce s3 plugin. I am not sure i customized the woocommerce to fetch the file name show in my account page here is the code.
function filename_wc_downloads( $link, $download )
{
$order = new WC_Order( $download['order_id'] );
$download_file_urls = $order->get_downloadable_file_urls(
$download['product_id'],
null,
$download['download_id']
);
foreach( $download_file_urls as $key => $value )
{
if( $value == $download['download_url'] )
{
$url_parts = explode( '/', parse_url( $key, PHP_URL_PATH ) );
$file_name = end( $url_parts );
$link = '<a href="'
. esc_url( $download['download_url'] )
. '">'
. $download['download_name']
. '</a> <small>( '
. $file_name
. ' )</small>';
}
}
return $link;
}
In a Woocommerce all products are uploaded into the media library of upload folder. The above code is to fetch the filename show in my account page if they using direct file path. If i pasted the above shortcode in the product url, that above code doesn't help to fetch the filename. so i need to know from the shortcode how can i get the object value based on this to show the file name.
amazon_s3 is your shortcode containing $atts bucket and object
when you use wordpress function add_shortcode('amazon_s3', 'your_function_name');
it automatically converts your attributes defined in [amazan_s3 .... to $atts
e.g.
function your_funnction_name($atts) {
extract(shortcode_atts(array(
'bucket' => '',
'object' => ''
), $atts));
return $object;
}

WordPress: Custom default avatar on localhost?

I'm trying to add a custom default avatar to WordPress in functions.php, but the image is not displaying in Settings/Discussion or elsewhere on the site. The code works because a new radio field is added with the custom field name, but the image won't display. Is the avatar not displaying because I'm using Localhost?
I don't have enough reps to comment on similar questions.
here's the code:
add_filter( 'avatar_defaults' , 'wps_new_avatar' );
function wps_new_avatar( $avatar_defaults ){
$new_avatar = get_stylesheet_directory_uri() . '/images/default-avatar.png';
$avatar_defaults[$new_avatar] = "Default Avatar";
return $avatar_defaults;
}
I've tried other examples and the 'Add-New-Default-Avatar' plugin with the same result.
I was facing the same issue and came up with this completely hackish solution... It works though :)
add_filter( 'get_avatar', 'so_14088040_localhost_avatar', 10, 5 );
function so_14088040_localhost_avatar( $avatar, $id_or_email, $size, $default, $alt )
{
$whitelist = array( 'localhost', '127.0.0.1' );
if( !in_array( $_SERVER['SERVER_ADDR'] , $whitelist ) )
return $avatar;
$doc = new DOMDocument;
$doc->loadHTML( $avatar );
$imgs = $doc->getElementsByTagName('img');
if ( $imgs->length > 0 )
{
$url = urldecode( $imgs->item(0)->getAttribute('src') );
$url2 = explode( 'd=', $url );
$url3 = explode( '&', $url2[1] );
$avatar= "<img src='{$url3[0]}' alt='' class='avatar avatar-64 photo' height='64' width='64' />";
}
return $avatar;
}
Result:
Of course, this filter is meant for development only.

How to change the attachment url in Wordpress

What I'm trying to do is change the link to the main attachment page in Wordpress. Basically, I'm trying to change the word attachment to media.
I'm trying to change:
example.com/parent-category/child-category/post-slug/attachment/attachment-name/
to:
example.com/parent-category/child-category/post-slug/media/attachment-name/
Thanks in advance for any help on this.
I know this is an old question, but I just stumbled on it and thought it was worth a shot;
function __filter_rewrite_rules( $rules )
{
$_rules = array();
foreach ( $rules as $rule => $rewrite )
$_rules[ str_replace( 'attachment/', 'media/', $rule ) ] = $rewrite;
return $_rules;
}
add_filter( 'rewrite_rules_array', '__filter_rewrite_rules' );
function __filter_attachment_link( $link )
{
return preg_replace( '#attachment/(.+)$#', 'media/$1', $link );
}
add_filter( 'attachment_link', '__filter_attachment_link' );

Resources