Way to get Twitter button, with count, with custom bit.ly URL, working? - wordpress

I'm stuck. I posted this on WordPress.StackExchange and they suggested I try at WebApps.StackExchange, and they suggested I try here. So, apologies for the multiple posts if you follow all those!
I have a client blog using bit.ly pro to generate custom short urls (ie foo.co). I want to show the regular horizontal version of the Twitter button, with tweet-count, and have the link that goes to the post use their custom bit.ly pro url.
I have installed Joost de Valk's Bit.ly Shortlinks plugin, which successfully converts normal WP shortlinks (wp_get_shortlink()) to the custom Bit.ly pro URL elsewhere in the site, but Twitter seems to trump that and render everything with the default t.co domain instead.
I've looked at the suggestions from this question but using the # as the data-url doesn't work, and the suggested Twitter support pages don't seem to contain any info on how to get Bit.ly to work (though they say they're going to).
Here's the function I created to insert the button in my theme - any ideas on where I'm going wrong? this is used to insert the button both within the Loop and on single-post pages.
function tweet_this() {
global $post;
ob_start();
$tweet = wp_get_shortlink();
echo '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
return ob_get_clean();
}
In case it helps, this function does work, except it doesn't render the tweet-count:
function tweet_this() {
global $post;
ob_start();
$tweet = sprintf( __('%1$s %2$s'), $post->post_title, wp_get_shortlink() );
echo '<a class="tweethis" href="http://twitter.com/intent/tweet?text=' . urlencode( $tweet ) . ' via #clientname">Tweet this</a>';
return ob_get_clean();
}
Let me know if you need more info - and thanks in advance for any help you can throw my way!
Michelle

function tweet_this() {
global $post;
$tweet = get_permalink(); //replace with your code
$tweetmarkup = '<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>Tweet';
echo $tweetmarkup;
}
This works for me, but I don't have the WPShortlinks installed, so I replaced it with the permalink. You should be able to replace the permalink with your wp_get_shortlink and it should work.

Related

Visual Composer Grid with do_action shortcode is not working

I have visual composer which is packed with total theme. When I put the following grid short code in my page in the editor it works correctly.
[vc_basic_grid post_type="post_type" max_items="10" item="masonryGrid_SlideFromLeft" grid_id="vc_gid:1458178666639-80ebf3775500c87d35de078c3422fe96-10" taxonomies="555"]
However, when I call the exact same code using do_action it gives the following javascript error. I checked the html output and it is the same using do_action like putting the short code in editor.
Error: Syntax error, unrecognized expression: {'status':'Nothing found'}
s
Any help is greatly appreciated.
Well, you can't output contents directly in your templates by using core shortcodes of VC like that.
1. Problem:
For security, besides nonce, VC uses page_id and shortcode_id to check AJAX request/respond data.
The shortcode_id is automatically generated by VC, you can not harcode it.
For example, this is the shortcode you see on admin editor screen:
[vc_basic_grid post_type="post_type" max_items="10" item="masonryGrid_SlideFromLeft" grid_id="vc_gid:1458178666639-80ebf3775500c87d35de078c3422fe96-10" taxonomies="555"]
Let say the page ID is 4269, this is the generated HTML code on front-end:
<!-- vc_grid start -->
<div class="vc_grid-container-wrapper vc_clearfix">
<div class="vc_grid-container vc_clearfix wpb_content_element vc_masonry_grid" data-initial-loading-animation="zoomIn" data-vc-grid-settings="{"page_id":4269,"style":"all-masonry","action":"vc_get_vc_grid_data","shortcode_id":"1458178666639-80ebf3775500c87d35de078c3422fe96-10","tag":"vc_masonry_grid"}" data-vc-request="http://example.com/wp-admin/admin-ajax.php" data-vc-post-id="4269" data-vc-public-nonce="0641473b09">
</div>
</div>
<!-- vc_grid end -->
Now, if page_id and shortcode_id don't match each other, {'status':'Nothing found - $shorcode_id'} will be throw out and no contents will be displayed.
You can find out more inside vc_grid.min.js file.
2. Solution:
Generate a fake page with VC, then copy generated html code to your template file.
Create a template with VC directly.
Use Shorcode Mapper to create your own shorcode.
First you build a new page and add a grid post on it,
then we get
_vc_post_settings
post meta , and try to build a new one
then update post meta data
now we can by pass VC Ajax security check
in the following code "1513628284966-37b8c3ca-d8ec-1" is VC generated guid
you should change it to yours .
$meta = get_post_meta(1365,'_vc_post_settings');
$settings = array();
#$settings['vc_grid_id'] = $meta[0]['vc_grid_id'];
$key = random_int(1513628284966,9513628284966);
$settings['vc_grid_id']['shortcodes'][''.$key.'-37b8c3ca-d8ec-1'] = $meta[0]['vc_grid_id']['shortcodes']['1513628284966-37b8c3ca-d8ec-1'];
$settings['vc_grid_id']['shortcodes'][''.$key.'-37b8c3ca-d8ec-1']['atts']['custom_query'] = "tag=shop";
$settings['vc_grid_id']['shortcodes'][''.$key.'-37b8c3ca-d8ec-1']['atts']['grid_id'] = ''.$key.'-37b8c3ca-d8ec-1';
$n = add_post_meta(1365,'_vc_post_settings',$settings);
return do_shortcode("[vc_basic_grid post_type=\"custom\" show_filter=\"yes\" filter_style=\"dropdown\" item=\"5959\" grid_id=\"vc_gid:".$key."-37b8c3ca-d8ec-1\" filter_source=\"post_tag\" custom_query='tag=".$tag."']");
I've solved.
I had the same problems, with the Visual Composer Editor offered by WpBakery
https://wpbakery.com/
and after understanding the connection between the IDs of the block, and the ID of the Post, I put more attention to the settings of the Block.
There is infact one field called "Element ID", and here we have to put our ID of the Post we are editing.
In my case the Block was a block containing some Posts.
After saving, and viewing the page without the Editor, I was finally able to see the block, and not the message
{"status":"Nothing found"}
I found a solution to this problem.
I modified the woocommerce category template and linked to the woocommerce_archive_description hook to add additional descriptions from some pages, for this I got their id, and then display the content.
echo do_shortcode($post->post_content);
The gallery(media grid) didn't work because there was a mismatch between the page id and the shortcode id. Therefore, the logical solution was to redefine the global variable $post to the $post of the page from which I get the content.
global $post;
$post = get_post( $id );
And it turns out that the post id matches.
After that, don't forget to return the normal $post value;
wp_reset_postdata();
By the way - use this option to load custom styles for wpbakery elements.
echo '<style type="text/css" data-type="vc_shortcodes-custom-css">' . get_post_meta( $id, '_wpb_shortcodes_custom_css', true ) . '</style>';
The whole code
function extra_product_category_desc(){
if( is_product_category() ){
$id = get_term_meta (get_queried_object()->term_id, 'pageId', true);
if($id !== ''){
global $post;
$post = get_post( $id );
echo do_shortcode($post->post_content);
echo '<style type="text/css" data-type="vc_shortcodes-custom-css">' . get_post_meta( $id, '_wpb_shortcodes_custom_css', true ) . '</style>';
wp_reset_postdata();
}
}
}
add_action( 'woocommerce_archive_description', 'extra_product_category_desc', 11 );
You may also try with do_shortcode('');
Like
do_shortcode('[vc_basic_grid post_type="post_type" max_items="10" item="masonryGrid_SlideFromLeft" grid_id="vc_gid:1458178666639-80ebf3775500c87d35de078c3422fe96-10" taxonomies="555"]');
Best Regards,

WordPress excerpt_more filter hook is not working only on the very first post of my blog page

I have set a blog page that is separate from my static home page.
I have modified the “[...]” link that comes after each post’s excerpt on the blog page by using a filter function.
To do this, I used WordPress’s “excerpt_more” hook.
add_filter('excerpt_more', 'new_excerpt_more');
function new_excerpt_more($more) {
global $post;
return '... <br clear="all"/><a class="moretag" href="'. get_permalink($post->ID) . '">Continue Reading</a>';
}
The problem I am having is that, for some reason, this works for every post, except the very first post of the blog page.
Is there something different or special about the very first post of the blog page that would make it not go through this filter hook?
Thank you very much for any help.
Try with this one:
add_filter('excerpt_more', 'new_excerpt_more');
function new_excerpt_more($more) {
return '... <br clear="all"/><a class="moretag" href="'. get_permalink( get_the_ID() ) . '">Continue Reading</a>';
}
Also, if in Wordpress a post has a manual excerpt, the excerpt_more filter wont get executed. So it's always recommended to check that, just in case.

is there a way (plugin) to insert html/css/images ect automatically into every wordpress post?

is there a way (plugin) to insert html/css/images ect.. automatically into every wordpress post? most of my posts are going to be very similar so is there a plugin that will automatically insert the pre written html/css/ ect in every post as opposed to me entering it manually every time.
Thank you in advance ;-)
You can write your own simple function for this, see my example below:
add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
global $post_type;
if( $post_type == 'post') { /* Or your custom post type, pages etc. */
$content = 'Your custom HTML/CSS content here';
}
return $content;
}
Place this in functions.php and it will be the default content of every new post/page/custom post type you create.
For a list of available post types, please refer to the Codex
You could use a plugin such as Ad injection, it will allow you to do what you need without having to alter / amend / ad any code to the templates or files

Drupal 7 - Theme the user profile form

I'm trying to theme my Drupal site's user profile form at the moment. I'm using hook_form_alter in the theme's template.php file.
The code is the same as the code I've used to edit another form but for some reason I can't spot it's not working.
function THEME_NAME_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_profile_form') {
$form['current_pass']['#prefix'] = '<div class="loginFormBlock">';
$form['current_pass']['#suffix'] = '</div>';
$form['current_pass']['#size'] = '500';
//$form['actions']['submit'] = array('#type' => 'image_button', '#src' => base_path() . path_to_theme() . '/images/Login.png');
}
}
Now the commented out submit button part works when it's un commented but the current_pass bits don't do anything. Current_pass is the name of the field I'm trying to theme. THEME_NAME has been replaced by the theme's name.
ANSWER:
As suggested I looked into the $form array.
echo '<pre>';
print_r($form);
echo '</pre>';
Saw that current_pass was in the account array and amended the code to the following, which works fine.
$form['account']['current_pass']['#size'] = '500';
Hopefully this can help someone else.
I was just going to say same thing - do a dump of $form and make sure that the field actually exists - if it does and it still doesn't work then there may be another hook being called after yours and changing your changes.
ps: I am still using D6 but are you sure your putting your form_alter function in the right place? yours says THEME_NAME_form_alter - I know in D6 you have to put them in module layer not the theme layer (unless this has changed in D7???) - could this be your issue?

targeting title in wordpress post

I am working on a wordpress plugin that modifies the title of a post. I only want to do this when I am viewing a single post. To be specific, I want to add a link beside the title, but for purposes of the question, I will be adding some arbitary text.
I started out by using the 'the_title' filter hook, and calling this function.
function add_button_to_title($title)
{
global $post;
if(is_single())
{
return $title.'googly googly';
}
return $title;
}
The problem is, the links on the side bar apparently also use 'the_title', as I saw my text showing up in the side bars as well, which led me to:
if(is_single() && in_the_loop())
But then, in my theme(and i suppose themes in general) there is a link to the previous post and next post, which also uses 'the title' filter. So finally I have:
if(is_single() && in_the_loop() && ($post->post_title == $title))
The last conditional basically makes sure that it is the title of the post that is being printed, not the title of the next or previous post. This works but I am not sure how well it will work given different themes...It seems like terribly hacked together. Any advice from wordpress gurus out there? I am worried that the title would be modified for other reasons and the conditional will fail.
Any help is appreciated!
Ying,
There isn't really a good solution except, as ShaderOp said, requiring theme modification. Your solution will work for the most part. The only exception is if the theme developer has changed the query in a page. I'd say this is probably a good enough solution that it'll cover more than 95% of the cases you'd run into.
I solved a similar issue by adding a check to see if the title being filtered matches the title of the post. This avoids the issue with other post titles on the page (in sidebar, menu) also getting filtered.
function add_button_to_title( $title ) {
global $post;
if( is_single() && $title == $post->post_title ) {
return $title . 'googly googly';
} else {
return $title;
}
}
Wouldn't it be easier to keep the original version of your add_button_to_title function, but instead of hooking it to a filter, call it directly from your single.php page in the appropriate place?
For example, somewhere in your theme's single.php, instead of this:
<h3 class="storytitle">
<?php the_title(); ?>
</h3>
Use this:
<h3 class="storytitle">
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php echo add_button_to_title(the_title('', '', false); ?>
</a>
</h3>
today I ran into a similar problem. the_title gets called several times accross the whole page (e.g., in the html-head, the menus, the sidebar). I followed a similar approach using conditionals and the post/page id.
Additionally, I added a boolean flag which is set to true using the 'the_content' filter. So the title gets changed until the content is displayed. This way, I ensure that sidebars/widgets are not affected (e.g. Thematic theme has a default widget with links to pages - here the other conditionals would not be helpful as get_the_id() would return an equivalent). This ONLY works if the theme uses sidebars on the right. I did not find a way yet how to hook in directly before the 'the_title' call for the page/post to enable the boolean flag.
function myplugin_adjust_title($title, $id) {
global $myplugin_title_changed;
if ($myplugin_title_changed) {
return $title;
}
if (in_the_loop() && is_page('myplugin') && $id == get_the_ID()) {
$title = '';
}
return $title;
}
add_filter('the_title', 'myplugin_adjust_title', 10, 2);
function myplugin_adjust_title_helper_content($content) {
global $myplugin_title_changed;
$myplugin_title_changed = true;
return $content;
}
add_filter('the_content', 'myplugin_adjust_title_helper_content');

Resources