I have the code below and i want it to display content on implementation using
[insert_dos]*Content for dos here*[/insert_dos]
[insert_donts]*Content for dos here*[/insert_donts]
Dos
Content for dos here
Don'ts
Content for don'ts here
The code am trying to use
// Shortcode for dos
function insert_dos_func( $atts,$content ) {
extract( shortcode_atts( array(
'content' => 'Hello World',
), $atts ) );
return '<h2>DOs</h2>';
return '<div>' . $content . '</div>';
}
add_shortcode( 'insert_dos', 'insert_dos_func' );
// Shortcode for don'ts
function insert_donts_func( $atts ) {
extract( shortcode_atts( array(
'content' => 'Hello World',
), $atts ) );
return "<h2>DON'Ts</h2>";
return "<div>" . $content . "</div>";
}
add_shortcode( 'insert_donts', 'insert_donts_func' );
The first issue you're going to face is the use of multiple return statements inside a single function. Anything after the first return won't be executed.
The second issue is the way you're passing in content. There's an element in your attributes array named content. If you run extract on that array it's going to override the $content argument of your shortcode callback.
function insert_dos_func( $atts, $content ) {
/**
* This is going to get attributes and set defaults.
*
* Example of a shortcode attribute:
* [insert_dos my_attribute="testing"]
*
* In the code below, if my_attribute isn't set on the shortcode
* it's going to default to Hello World. Extract will make it
* available as $my_attribute instead of $atts['my_attribute'].
*
* It's here purely as an example based on the code you originally
* posted. $my_attribute isn't actually used in the output.
*/
extract( shortcode_atts( array(
'my_attribute' => 'Hello World',
), $atts ) );
// All content is going to be appended to a string.
$output = '';
$output .= '<h2>DOs</h2>';
$output .= '<div>' . $content . '</div>';
// Once we've built our output string, we're going to return it.
return $output;
}
add_shortcode( 'insert_dos', 'insert_dos_func' );
Related
I use this plugin to filter my searches.
in this section they explain how to create my own custom block.
function prefix_register_block( $blocks ) {
// 'my_block' corresponds to the block slug.
$blocks['my_block'] = [
'name' => __( 'My Block', 'text-domain' ),
'render_callback' => 'prefix_my_block_render',
];
return $blocks;
}
add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );
// The render callback function allows to output content in cards.
function prefix_my_block_render() {
// Get current post, term, or user object.
$post = wpgb_get_post();
// Output the post title.
echo '<h3>' . esc_html( $post->post_title ) . '</h3>';
}
the code looks simple, so it's a matter of common sense and a php expert can figure it out quickly.
I would like to add a line to say
if slug or term "office" exist in the property-type taxnonomy
then Output the post title
(which I will of course edit to display custom fields)
but how to make conditional logic possible?
thank you for your patience, I do my best to understand the correct technique
I'm not a WP guru, but I think it should be this simple. Set up an array of slugs you want to affect in this way and then iterate through them in a foreach loop
function prefix_register_block( $blocks ) {
$slugs = ['office','somethingelse'];
foreach($slugs as $slug) {
$blocks[$slug] = [
'name' => __( ucwords($slug), 'text-domain' ),
'render_callback' => 'prefix_my_block_render',
];
}
return $blocks;
}
add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );
// The render callback function allows to output content in cards.
function prefix_my_block_render() {
// Get current post, term, or user object.
$post = wpgb_get_post();
// Output the post title.
echo '<h3>' . esc_html( $post->post_title ) . '</h3>';
}
I've tried everything I can think of. the title spits out at the top of the content wrapper and the html tags stay where they should.
I'm performing this shortcode from a page. I need the title and perma link to an image in the media library so that I can spit out html to show it on a page via shortcode
add_shortcode( 'ispimg', 'isp_gallery_item' );
function isp_gallery_item( $atts ) {
// Attributes
$a = shortcode_atts( array(
'id' => '',
), $atts );
$isp_post_id = get_post($a['id']);
setup_postdata($isp_post_id);
$pt = the_title();
return "<h3>".$pt."</h3>";
wp_reset_postdata();
}
Not totally sure if this is your issue, but I suggest you not to mess with the main query, you may be affecting other parts of your page because of that.
Use the API functions like get_the_title() instead:
function isp_gallery_item( $atts ) {
// Attributes
$a = shortcode_atts( array(
'id' => '',
), $atts );
return sprintf( '<h3>%s</h3>', get_the_title( $a['id'] ) );
}
add_shortcode( 'ispimg', 'isp_gallery_item' );
Or in case you need the loop, take a look at the WP_Query class.
I've run into a little situation that is hopefully possible to solve. My goal is to take an existing XML file from the server, parse it, and then inject it as a list into Wordpress's original WYSIWYG editor so that the site owner has the list readily available when he writes a new post. Right now, I have this code in my wp-admin/edit-form-advanced.php file:
/**
* Fires after the title field.
*
* #since 3.5.0
*
* #param WP_Post $post Post object.
*/
do_action( 'edit_form_after_title', $post );
if ( post_type_supports($post_type, 'editor') ) {
?>
<div id="postdivrich" class="postarea<?php if ( $_wp_editor_expand ) { echo ' wp-editor-expand'; } ?>">
<?php
/** LOAD XML FROM SERVER AND PARSE AS UL INTO EACH NEW WP POST **/
$xml = simplexml_load_file('../my-folder/file.xml');
$product = "<br/><br/><h2 style='text-align:center; color:#003300;'><u>Products Available Now</u></h2><br/><ul style='text-align:center; list-style:none; color:#003300;'>";
foreach( $xml as $value ) {
$product .= "<li>";
$product .= $value->Description .= " $";
$product .= $value->Price .= " / ";
$product .= $value->QtyUnit .= "\n";
$product .= "</li>";
};
?>
<?php wp_editor($product, 'content', array(
'_content_editor_dfw' => $_content_editor_dfw,
'drag_drop_upload' => true,
'tabfocus_elements' => 'content-html,save-post',
'editor_height' => 300,
'tinymce' => array(
'resize' => false,
'wp_autoresize_on' => $_wp_editor_expand,
'add_unload_trigger' => false,
),
) ); ?>
Although it works, this causes a couple issues.
1) It injects the data into every WYSIWYG editor, including pages, which I would like to avoid. The content should only appear in post editors if possible.
2) It causes a pretty serious bug that erases anything but the list whenever that particular admin page is reloaded. I can't save any drafts, or edit posts or pages unless I keep that session open in the browser during the editing process.
Not sure if these issues can be solved, but any and all help is sincerely appreciated!!
You should never modify WP core files. It's advisable that you update or restore the original files.
What you need can be achieved with this little plugin:
<?php
/**
* Plugin Name: Default Post Content
*/
add_action( 'load-post-new.php', 'new_post_so_44123076' );
function new_post_so_44123076() {
# Only load if post type not defined (only occurs for Posts)
if( isset($_GET['post_type']) )
return;
add_filter( 'default_content', 'default_content_so_44123076' );
}
function default_content_so_44123076( $content ) {
# Build your own custom content
$content = "My html content.";
return $content;
}
Make a folder for the plugin, put the code inside a file (custom-content.php) and put the XML on the same folder.
It can be retrieved like this:
$xml = plugins_url( '/file.xml', __FILE__ );
I was wondering if I can use two short codes in 1. I know you can replace variables in shortcodes but what I'm looking for is to use a shortcode say [apple] which will provide a link to the link I give in the shortcode but then if I want an icon next to that link be able to use [apple icon] which than provide a link with the icon to the left of it.
add_shortcode('apple', 'apple');
function apple()
{
return '<a href="http://example.com/apple>Apple</a>';
}
So is it possible to add icon to where if we add icon to the short code it will also return an image too which I would specify in the shortcode.
You will want to use attributes.
add_shortcode( 'apple', 'apple' );
function apple( $atts, $content = null )
{
extract( shortcode_atts( array( 'icon' => 'false'), $atts ) );
if($icon == 'true'){
$output = '<img src="apple.png" />';
}
$output .= 'Apple';
return $output;
}
You would then call it like
[apple icon="true"]
Here is the documentation https://codex.wordpress.org/Function_Reference/add_shortcode
If I understand your question correctly you shouldn't need to create a shortcode in a shortcode. The shortcode is already executing within your plugin/functions so you should be able to simply invoke your function (or the icon function) directly within your shortcode handler function. No need to run it through shortcodes again.
If it's not your own shortcode you are looking to call you can call do_shortcode
http://codex.wordpress.org/Function_Reference/do_shortcode
To do what you want, it's not necessary to have two shortcodes, just make your shortcode accept arguments:
add_shortcode('apple', 'apple');
function apple($args) {
$default = array('icon' => '');
$args = wp_parse_args($args, $default);
$content = '';
if ($args['icon']) {
$content.= '<img src="icon.png">';
}
$content.= '<a href="http://example.com/apple>Apple</a>';
return $content;
}
To use this, you would then enter your shortcode as follows:
[apple icon="yes"]
If it's simply a "yes/no" for the icon.
Or, you could make it so it loads the icon dynamically based on what you set icon equal to, which would require some modifications to the function:
function apple($args) {
$default = array('icon' => '');
$args = wp_parse_args($args, $default);
$content = '';
if ($args['icon']) {
$content.= '<img src="' . $args['icon'] . '">';
}
$content.= '<a href="http://example.com/apple>Apple</a>';
return $content;
}
And your shortcode would then look like so:
[apple icon="my_icon.png"]
(Note, you'd probably need to pass in a fully qualified domain name, like so: [apple icon="http://example.com/images/my_icon.png"])
I'm trying to use a widget within a plugin in wordpress and I'm seeing this error within the widget box:
Warning: extract() [function.extract]: First argument should be an array in /nfs/c03/h04/mnt/57957/domains/rab.qbessi.com/html/wp-content/plugins/register-plus/dash_widget.php on line 24
This is the code from Line 24:
// Output the widget contents
function widget( $args ) {
extract( $args, EXTR_SKIP );
Here's the dash_widget.php code
<?php
if( !class_exists('RegisterPlusWidget') ){
class RegisterPlusWidget{
function RegisterPlusWidget() { //contructor
// Add the widget to the dashboard
add_action( 'wp_dashboard_setup', array($this, 'register_widget') );
add_filter( 'wp_dashboard_widgets', array($this, 'add_widget') );
}
function register_widget() {
wp_register_sidebar_widget( 'regplus_invite_tracking', __( 'Invitation Code Tracking', 'regplus' ), array($this, 'widget'), array( 'settings' => 'options-general.php?page=register-plus' ) );
}
// Modifies the array of dashboard widgets and adds this plugin's
function add_widget( $widgets ) {
global $wp_registered_widgets;
if ( !isset($wp_registered_widgets['regplus_invite_tracking']) ) return $widgets;
array_splice( $widgets, 2, 0, 'regplus_invite_tracking' );
return $widgets;
}
// Output the widget contents
function widget( $args ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
echo $before_title;
echo $widget_name;
echo $after_title;
global $wpdb;
$regplus = get_option( 'register_plus' );
$codes = $regplus['codepass'];
$usercodes = array();
foreach($codes as $code){
$users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key='invite_code' AND meta_value='$code'" );
echo '<h3>' . $code . ': <small style="font-weight:normal">' . count($users) . ' Users Registered.</small></h3>';
}
echo $after_widget;
}
}
} # End Class RegisterPlusWidget
// Start this plugin once all other plugins are fully loaded
add_action( 'plugins_loaded', create_function( '', 'global $regplus_widget; $regplus_widget = new RegisterPlusWidget();' ) );
?>
The widget() function is being called with no parameters. Why, is hard to tell without digging deeply into the plugin. You should ask the plugin's author.
You can try adding
// Output the widget contents
function widget( $args ) {
if (is_array($args)) // Add this
extract( $args, EXTR_SKIP );
and see whether the output still makes sense then. That effectively just suppresses the action that causes the warning. If it's a badly programmed plugin that was developed with warnings turned off, that already may do the trick.
google launched new plugin called site kit, it will make analyzing work much more easier.
https://wordifact.com/google-site-kit/