shortcode doesn't work wordpress - wordpress

I have this shortcode code..
function codeThree($attr,$content) {
extract(shortcode_atts(array(
'style' => 'design1',
'title' => 'Here is where the title will be reside..',
'image' => 'codethreeimg'
), $atts));
$dir = get_template_directory_uri();
return '<div class="codethree extend" style="background: transparent url('.$dir.'/images/'.$style.'.jpg) no-repeat top center;"><img class="codethreeimg" src="'.$dir.'/'.$image.'" /><p class="codethreep"><h2 class="codethreetitle">'.$title.'</h2><br/>'.do_shortcode($content).'</p></div>';
}
function register_shortcodes(){
add_shortcode('codethree', 'codeThree');
}
add_action( 'init', 'register_shortcodes');
and this for displaying them..
[codethree style="design1" title="Hi, I'm Armando Gutierrez, #1 Personal Trainer in Torrance & LA, and Body Transformation Specialist." image="http://localhost/wordpress/wp-content/uploads/2013/09/Profile.png"]some contents here[/codethree]
as you can see, this:
'style' => 'design1',
'title' => 'Here is where the title will be reside..',
'image' => 'codethreeimg'
will be default, if user doesn't specify there define content on those array fields, but what happens, the default content was instead displayed although those fields has been filled by the user define content.
[codethree style="design1" title="Hi, I'm Armando Gutierrez, #1 Personal Trainer in Torrance & LA, and Body Transformation Specialist." image="http://localhost/wordpress/wp-content/uploads/2013/09/Profile.png"]some contents here[/codethree]
Could someone check my codes and at least tell me whats wrong please? I'm open to any suggestions, ideas and recommendations! Thank you in advance.

I don't believe you need the add_shortcode to be attached to a hook. Instead of:
function register_shortcodes(){
add_shortcode('codethree', 'codeThree');
}
add_action( 'init', 'register_shortcodes');
Try just add_shortcode('codethree', 'codeThree'); and lose the rest. At least, that's how I've always done mine.

Since you have only one shortcode why creating a function to register? why don't you just use the add_shortcode('codethree', 'codeThree');
here is the example of one of my shortcodes that i do not even to my plugins but to accomplish anything i want this one is just for making a content in a post visible only to the non registered members of the site
add_shortcode('wageni','for_guests_only');
function for_guests_only($atts,$content){
if (!is_user_logged_in()){
return $content;
}
return '';
}

Related

Wordpress custom meta box for one page only

everyone!
I believe similar question have been asked already, but I had not found working solution.
The problem: I need a metabox for one exact page only.
The code I use to add a metabox is pretty simple:
function custom_meta_boxes(){
add_meta_box('custom_meta_1', 'About Us Main field','custom_meta_boxes_render',
'page','normal','high','');
}
If you look on the 4th parameter - 'page', this puts metabox on every page, so that I see it and can use it for every page I created when editing it. And I need to see it on about us page only (for example).
I`ve seen in some tutorials that instead of 'page' you can use an ID or a slug of page, but that did not work for me.
Therefore, I really need your help/advice on that.
Thanks a lot in advance.
Try to do it like this:
<?php
function custom_meta_boxes(){
global $post;
$postSlug = isset($post->slug) ? $post->slug : '';
if ($post->slug === 'about-us') {
add_meta_box(
'custom_meta_1',
'About Us Main field',
'custom_meta_boxes_render',
'page',
'normal',
'high',
''
);
}
}
And change about-us slug for that you want to add meta box.

Wordpress remove_meta_box issue

I run into a problem trying to get remove_meta_box() to work
/** remove metabox for catchkathmandu options
*/
function vpm_remove_meta_box() {
remove_meta_box( 'catchkathmandu-options', 'post', 'normal' );
remove_meta_box( 'authordiv' , 'page' , 'normal' ); //removes author
}
add_action( 'add_meta_boxes', 'vpm_remove_meta_box' );
The point is I dont want contributors and authors to edit the site design, so I wanted to remove the catchkathmandu-options. the code obviously does not reflect the condition, the codex for this function supplies the solutions for that but this code at the besic step, still isnt working - I switched back to Twenty Sixteen theme and put in the authordiv section simply to test. But still no dice.
It's added to child theme functions.php
Have I taken the wrong path and am I looking at the wrong thing entirely?
Any help appreciated!
I suppose that you are using a theme called "Catch katHmandu", and you want to remove a meta box for everyone except Admins.
Here is the usage of remove meta box:
remove_meta_box( $id, $page, $context );
And the Reference on the codex: https://codex.wordpress.org/Function_Reference/remove_meta_box
As you can read on the codex, $id is the current id of the div that you want to remove. Let's say that you want to remove "Tags box", if look for its container id you will find:
<div id="tagsdiv-post_tag" class="postbox">
...
</div>
And if you want to remove tags meta box, your code will look like this:
if (!is_admin()) :
function my_remove_meta_boxes() {
remove_meta_box('tagsdiv-post_tag', 'page', 'normal');
}
add_action( 'admin_menu', 'my_remove_meta_boxes' );
endif;
Look for the id of the meta box that you want to remove and remplace "tagdiv-post_tag" from code above with your id.
If you like my answer click the top arrow, as a coder it would mean a lot for me. Thanks!
EDIT:
As #AndrewSeabrook say, try using admin_menu hook instead of add_meta_boxes in your function.

Adding a new BuddyPress menu item

add_action( 'bp_setup_nav', 'test_board', 100 );
function test_board() {
global $bp;
bp_core_new_nav_item( array(
'name' => 'Test Board',
'slug' => 'test-board',
'screen_function' => 'bpis_profile',
'position' => 10
)
);
}
function bpis_profile () {
echo do_shortcode('[bpis_tags]');
echo do_shortcode('[bpis_images]');
}
The issue is that when I click on this link in the BP nav bar, it outputs the shortcodes (as per the bpis_profile function) outside of any divs (meaning it just appears at the top of the website, outside of the theme). In addition, the nav bar disappears and I receive the “About” page of the user I am currently viewing (which shows subscribed forum topics, favorite forum topics, etc).
Is there any workaround for this? Ideally, I’d like my shortcodes to output in the body region, beneath the BP nav bar (which has currently disappeared).
Thank you!
Try doing this:
function bpis_profile() {
bp_core_load_template( 'buddypress/members/single/posts' );
}
Then you can create a new file under buddypress/members/single/ called posts.php. Inside that file you can use get_header and get_footer to output your whole page. Of course, be sure to include your shortcodes too.

Redirect from add_menu_page

I have added a menu using add_menu_page which all works correctly, but when clicked I want this menu page to open the post editor for a particular post id.
As a proof of concept i have tried echoing a javascript redirect out into the do function like so...
// Load up the menu page
function register_availability_custom_menu_page() {
add_menu_page('custom menu title', 'Availability', 'add_users', 'options', 'options_do_page');
}
function options_do_page() {
echo "<script type=\"text/javascript\">";
echo "window.location = '/whatever_page.php';";
echo "</script>";
}
This approach does work but I was wondering if it is the best approach, is there a better way to redirect to the page I am after?
UPDATE
I have now also tried using wp_redirect with this code...
add_action( 'admin_menu' , 'admin_menu_new_items' );
function admin_menu_new_items() {
wp_redirect( home_url() );
exit;
}
This gives me a Headers already sent error, can anyone suggest where I am going wrong?
If I'm understanding this correctly, you don't need the redirect. Instead of using a $menu_slug in the function add_menu_page, put the address of the target page, e.g.:
$the_post_title = 'The Portfolio';
add_action( 'admin_menu', 'wpse_59050_add_menu' );
function wpse_59050_add_menu()
{
global $the_post_title;
$our_page = get_page_by_title( $the_post_title );
$settings_page = add_menu_page(
'Edit '.$our_page->post_title,
'Edit '.$our_page->post_title,
'add_users',
'/post.php?post='.$our_page->ID.'&action=edit',
'',
'',
2
);
}
This function is from the following WordPress Answer: Highlighting a Menu Item by Post Name. You'll need some jQuery to do the correct highlighting of this top level menu, check both my and TheDeadMedic answers in that Q.
This other one is useful too: Add highlighting to new Admin Dashboard Menu Item.
Recently did this for pootle page builder, and this AFAIK is the best way,
/**
* Redirecting from Page Builder > Add New page
* #since 0.1.0
*/
function add_new() {
global $pagenow;
if ( 'admin.php' == $pagenow && 'page_builder_add' == filter_input( INPUT_GET, 'page' ) ) {
header( 'Location: ' . admin_url( 'whatever-page.php' ) );
die();
}
}
add_action( 'admin_init', 'add_new' );
Replace page_builder_add with your page slug and admin_url( 'whatever-page.php' ) with url you wanna redirect to.
We always use scrutinizer-ci for best code practices and maintain code score greater than 9.5/10, so all code (Including this) is secure and optimized ;)
Lemme know how it works for ya.
Cheers
I found a 2-step solution that doesn't use JS/JQ.
Step 1:
Near the top of your script, put the following PHP to display CSS that hides your first page:
add_action('admin_head', 'hide_my_first_page');
function hide_my_first_page(){
echo '<style>
a[href="admin.php?page=admin_menu_slug"] + ul > li.wp-first-item{
display: none;
}
</style>';
}
Where admin_menu_slug is the 4th argument passed to add_menu_page();.
Step 2:
Take the function of the page you want to run and pass it as the 5th argument in add_menu_page();

Changing tag cloud link to location

I am learning Drupal as I go and I am wondering if I can change a link coming from out of a tag cloud.
The link coming from the tag cloud goes to ...category/articles/locations/kittys
I would like it to go to the node tag at ...content/kittys
Any thoughts?
The assummed module you used for this, tagadelic, uses default taxonomy-paths for that.
So the answer is "yes" it can be changed. e.g. Forums (which are terms/tags in a taxonomy too) in a tagcloud will link to the forum home, not to the forum overview. This works because tagadelic uses taxonomy_term_path().
However, your question is a bit unclear about what (and why) you want to achieve this. What is "content/kitties"? Your question makes me believe you want to link to a node? Why? Tag-clouds represent tags, where the tag links to the list of posts within that tag.
That said, the easy way to change outgoing links is in the theme_function: to override the theme function.
/**
* theme function that renders the HTML for the tags
* #ingroup themable
*/
function my_custom_chees_puff_theme_tagadelic_weighted($terms) {
$output = '';
foreach ($terms as $term) {
$output .= l($term->name, "/link/to/anywere", array(
'attributes' => array(
'class' => "tagadelic level$term->weight",
'rel' => 'tag',
'title' => $term->description,
)
)
) ." \n";
}
return $output;
}
The other option is to override the general "where should a tag-link-link-to" Drupalwide. As forementioned forum.module does, trough hook_term_path():
function my_cheesy_puffs_kitten_module_term_path($term) {
return 'links/to/kittens/' . $term->tid;
}
Success! Bèr Kessels - Author and maintainer of Tagadelic :)

Resources