I am beginning to make a site in wordpress (so no php or html experience). I want to generate textlist from a mysql column into a text block.
I have made the php code for the shortcode below which returns an array which now displays in the textblock as "Array" instead of a list of strings.
If i just print the values they appear on the head of the page.
What are the next steps I need to do/look for. I can't find it because I probably do not know the correct search terms. My guess is something with HTML.
<?php
function location_marker_shortcode( $atts ) {
$a = shortcode_atts( array(
'mapnumber' => 'world'
), $atts );
global $wpdb;
//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');
foreach ( $marker_labels as $marker_label )
{
//print labels
echo $marker_label;
}
return $marker_labels;
}
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>
I have now this code which gives me a list exactly what i want but not in the "paragraph block" in wordpress where my shortcode is situated.
<?php
function location_marker_shortcode( $atts ) {
$a = shortcode_atts( array(
'mapnumber' => 'world'
), $atts );
global $wpdb;
//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');
foreach ( $marker_labels as $marker_label )
{
echo '<li>'. $marker_label.'</li>';
}
}
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>
I'm not 100% sure what you're trying to accomplish, but try this. You don't want to echo out all the values as you're looping through them. Concatenate everything in a variable and then return the entire string at the end of your shortcode. This should generate an unordered list.
<?php
function location_marker_shortcode( $atts ) {
$a = shortcode_atts( array(
'mapnumber' => 'world'
), $atts );
global $wpdb;
//select databases (the 84 part should be the input of the shortcode)
$marker_labels = $wpdb->get_col('SELECT label FROM wp_mapsvg_database_84');
$output = '<ul>';
foreach ( $marker_labels as $marker_label )
{
$output .= '<li>' . $marker_label . '</li>';
}
$output .= '</ul>';
return $ouput;
}
//add shortcode to wordpress
add_shortcode( 'matthijs', 'location_marker_shortcode' );
?>
Related
Idd like to add a class within the output of this piece of php code
function acf_esg_tax_field_loc( $atts, $content = null ) {
$a = shortcode_atts(array('post_id' => "1"), $atts);
$term = get_field( "field_56cb3d84cf32a", $a['post_id'] );
echo $term->name;
}
add_shortcode( 'acfesgtax_loc', 'acf_esg_tax_field_loc' );
The code is used in my Wordpress functions php to generate a shortcode.
It should be something like this
function acf_esg_tax_field_loc( $atts, $content = null ) {
$a = shortcode_atts(array('post_id' => "1"), $atts);
$term = get_field( "field_56cb3d84cf32a", $a['post_id'] );
echo '<div class="OutputClass" >';
echo $term->name;
echo '</div>';
}
add_shortcode( 'acfesgtax_loc', 'acf_esg_tax_field_loc' );
Unfortunatly this does not work.
Can you please help?
As the WordPress Code Reference states.
Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.
So first change it from echo to one return.
You could define a variable, concatenate to it and finally return it.
Result:
function acf_esg_tax_field_loc( $atts, $content = null ) {
$a = shortcode_atts(array('post_id' => "1"), $atts);
$term = get_field( "field_56cb3d84cf32a", $a['post_id'] );
$sR = '<div class="OutputClass" >';
$sR .= $term->name;
$sR .= '</div>';
return $sR;
}
add_shortcode( 'acfesgtax_loc', 'acf_esg_tax_field_loc' );
If the class is always the same and thus could be hardcoded, then you are very close. Only one echo will be outputted, so concatenate it:
echo '<div class="OutputClass" >'.$term->name.'</div>';
If it's dynamic and you need to get the class name somewhere and pass it along, then that's a whole different story.
I am using a Repeater of Advanced custom fields for the content of the my additional custom WooCommerce Tab. The repeater is inside a group field.
I manage to display the custom fields that is outside the repeater field.
Here is the code I used in my functions.php:
add_filter( 'woocommerce_product_tabs', 'dl_custom_product_designer_tab' );
function dl_custom_product_designer_tab( $tabs ) {
// ensure ACF is available
if ( !function_exists( 'have_rows' ) )
return;
if ( get_field('designer') ) {
$tabs[] = array(
'title' => 'DESIGNER',
'priority' => 50,
'callback' => 'dl_custom_designer_tab'
);
}
return $tabs;
}
function dl_custom_designer_tab() {
$designer = get_field('designer');
echo '<p>'.$designer['designer_image'].'</p>';
echo '<p>'.$designer['designer_name'].'</p>';
echo '<p>'.$designer['designer_short_description'].'</p>';
// loop through the rows of data
$achievements = get_field('designer_achievements');
if( $achievements ) {
// loop through the rows of data
echo '<ul>';
foreach($achievements as $achievement){
// display a sub field value
echo '<li>'.$achievement['achievement'].'</li>';
}
echo '</ul>';
}
}
Now the problem is the field inside my repeater field: The repeater sub field is not displaying anything.
What I am doing wrong? How can I have the output for the repeater sub fields?
Edit: A screenshot of the ACF settings repeater field.
UPDATE (new functional alternative):
Apparently this doesn't works with "product" post type…** This looks like a bug in this plugin (I have been able to test the same case and to reproduce the issue).
It should be reported to the authors support treads… I have do it on my side.
A temporary solution (as long as this bug is not solved by ACF team)
This is is custom function replacement for ACF repeater dedicated functions:
/**
* Custom function: Get an array of ACF repeater sub-field.
*
* #param string $master_field (the
* #param string $repeater_field
* #param array $sub_fields
* #output formatted html
*/
function repeater_subfield( $group_name, $repeater, $subfield ){
global $post, $product;
$repeater_meta_key = $group_name.'_'.$repeater;
$rows = get_post_meta( $post->ID, $repeater_meta_key, true );
for($i = 0; $i < $rows; $i++){
$subfield_meta_key = $repeater_meta_key.'_'.$i.'_'.$subfield;
$output[] = get_post_meta( $post->ID, $subfield_meta_key, true );
}
if( count($rows) > 0 ) return $output;
else return;
}
Then your tested and functional code should be:
// Add a custom product tab
add_filter( 'woocommerce_product_tabs', 'dl_custom_product_designer_tab' );
function dl_custom_product_designer_tab( $tabs ) {
// ensure ACF is available
if ( !function_exists( 'have_rows' ) )
return;
if ( get_field('designer') ) {
$tabs[] = array(
'title' => 'DESIGNER',
'priority' => 50,
'callback' => 'dl_custom_designer_tab'
);
}
return $tabs;
}
// The custom product tab content
function dl_custom_designer_tab() {
global $post, $product;
$group_name = 'designer';
$designer = get_field( $group_name );
echo '<p>'.$designer['designer_image'].'</p>';
echo '<p>'.$designer['designer_name'].'</p>';
echo '<p>'.$designer['designer_short_description'].'</p>';
$designer_achievements = repeater_subfield( $group_name, 'designer_achievements', 'achievement' );
// check if the repeater field has rows of data
if( count($designer_achievements) > 0 ):
echo '<ul>';
// loop through the rows of data
foreach( $designer_achievements as $achievement ){
// display a sub field value
echo '<li>'.$achievement.'</li>';
}
echo '<ul>';
else:
// "no rows found" optional message
echo '<p><em>No data…</em></p>';
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works…
Original answer:
With Advanced Custom Field Pro plugin to get the Repeater sub fields data, you will need to use the documented way, functions and methods have_rows() and get_sub_field(), this way:
function dl_custom_designer_tab() {
global $post;
$designer = get_field('designer');
echo '<p>'.$designer['designer_image'].'</p>';
echo '<p>'.$designer['designer_name'].'</p>';
echo '<p>'.$designer['designer_short_description'].'</p>';
// check if the repeater field has rows of data
if( have_rows('designer_achievements') ):
echo '<ul>';
// loop through the rows of data
while ( have_rows('designer_achievements') ) : the_row();
// display a sub field value
echo '<li>' . get_sub_field('achievement') . '</li>';
endwhile;
echo '<ul>';
else:
// "no rows found" optional message
echo '<p><em>No row founds in repeater…</em></p>';
endif;
}
my shortcode output always appear on the top of my custom template.
custom template
$tag= 'the_content';
remove_all_filters( $tag);
$postid = get_the_ID();
$post = get_post($postid);
$content = do_shortcode($post->post_content);
ob_start();
echo $content;
$result = ob_get_contents();
ob_end_clean();
return $result;
custom shortcode
function signupform_shortcode( $atts ) {
extract( shortcode_atts( array(
'socialmkt' => 'aweber'
), $atts ) );
if($socialmkt == 'aweber'){
if($display == 'popup') {
return include_once('modal-aweber.php');
}
}
}
add_shortcode('signupform', 'signupform_shortcode');
The shortcode it's place in the middle of a html landing.
I try adding ob_start() that i read in other posts but still not working.
Your shortcode callback is going to output content rather than return it which is why you're seeing it appear at the top of your page.
Using output buffering (ob_start() / ob_get_contents()) is a valid way of addressing the problem however you need to move the code.
Output buffering should be taking place inside your shortcode callback where the return value is required instead of output.
function signupform_shortcode( $atts ) {
extract( shortcode_atts( array(
'socialmkt' => 'aweber'
), $atts ) );
// Begin output buffering here. Any output below will be stored in a buffer.
ob_start();
if ( $socialmkt == 'aweber' ) {
if ( $display == 'popup' ) {
// Return, as previously used, doesn't help in this context.
include_once( 'modal-aweber.php' );
}
}
// Return (and delete unlike ob_get_contents()) the content of the buffer.
return ob_get_clean();
}
add_shortcode( 'signupform', 'signupform_shortcode' );
I'm not good with PHP.
I use "Advanced Categories Widget" to list categories on Sidebar.
I used this plugin because it offers the ability to display images categories.
But I need to order categories by random.
I find this code on the plugin:
function advanced_categories_widget_html( $args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new Walker_Advance_Category_Widget;
$output = wp_list_categories( $args );
if ( $output ) return $output;
}
and i Find another code on forum which displays correctly categories by random order:
wp_list_categories
how I can exploit the second code to hack the first code to list my categories with random order?
The PHP file for the plugin: http://codepad.org/a3yU7Xny
Accordion to the documentation on the Advanced Categories Widget plugin you're using -- -- you can specify 'orderby' in your plugin settings. See this screenshot.
You should have a random or rand option in the drop down.
Can't confirm cuz it's a paid plugin.
Just add the "hack" the function in the plugin file :
function advanced_categories_widget_html( $args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new Walker_Advance_Category_Widget;
$cats ='';
$categories=get_categories();
$rand_keys = array_rand($categories, 5); // 5 is the number of categories you want
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
}
$output = wp_list_categories($args.'&include='.$cats);
if ( $output ) return $output;
}
Or by a cleaner way, add in your functions.php file :
function random_advanced_categories_widget_html( $args = array() ) {
$args = wp_parse_args( $args );
$args['walker'] = new Walker_Advance_Category_Widget;
$cats ='';
$categories=get_categories();
$rand_keys = array_rand($categories, 5); // 5 is the number of categories you want
foreach ($rand_keys as $key) {
$cats .= $categories[$key]->term_id .',';
}
$output = wp_list_categories($args.'&include='.$cats);
if ( $output ) return $output;
}
There is a way to delete a specific shortcode, maintaining the text inside?
For example: in this case [dropcap]A[/dropcap] I would like to eliminate the shortcode maintaining the "A", or any other letter inside.
Thanks!
Use this JS regex to strip out any characters between square brackets from the text, while leaving any text that might've come beween shortcode tags, like [dropcap]A[/dropcap].
var myReg = /\[.+\]/g;
paragraphText = paragraphText.replace(myReg, '');
or to remove a shortcode like:
[media-credit param=value param2="value2"]text you actually want goes here[/media-credit]
You can use the following to your functions.php file:
add_filter( 'the_excerpt', 'remove_media_credit_from_excerpt' );
function remove_media_credit_from_excerpt( $excerpt ) {
return preg_replace ('/\[media-credit[^\]]*\](.*)\[\/media-credit\]/', '$1', $excerpt);
}
Here's a plugin that will launch one time and parse ALL POSTS' content, stripping the shortcodes (and leaving the content) of any desired shortcodes. Simply enter the shortcodes you want to strip in the $shortcode_tags array and the post type you want to perform on in the $posts array.
NOTE: This will impact your database and can NOT be undone. It is highly recommended that you backup your database first.
<?php
/*
Plugin Name: Strip Shortcodes Example
*/
add_action( 'init', '_replace_shortcodes' );
function _replace_shortcodes() {
global $shortcode_tags;
// Make sure this only happens ONE time
if ( get_option( '_replace_shortcodes_did_once' ) !== false ) {
return;
}
update_option( '_replace_shortcodes_did_once', true );
add_action( 'admin_notices', '_replace_shortcodes_notice' );
// Get all of our posts
$posts = get_posts( array(
'numberposts' => -1,
'post_type' => 'post', // Change this for other post types (can be "any")
));
// Make WP think this is the only shortcode when getting the regex (put in all shortcodes to perform on here)
$orig_shortcode_tags = $shortcode_tags;
$shortcode_tags = array(
'dropcap' => null,
);
$shortcode_regex = get_shortcode_regex();
$shortcode_tags = $orig_shortcode_tags;
// Loop through the posts
if ( ! empty( $posts ) ) {
foreach ( $posts as $post ) {
// Perform Regex to strip shortcodes for given shortcodes
$post_content = preg_replace_callback( "/$shortcode_regex/s", '_replace_shortcodes_callback', $post->post_content );
// Update our post in the database
wp_update_post( array(
'ID' => $post->ID,
'post_content' => $post_content,
) );
}
}
}
function _replace_shortcodes_callback( $matches ) {
// This is the shortcode content
$content = $matches[5];
// No content, so just return the whole thing unmodified
if ( empty( $content ) ) {
return $matches[0];
}
// Otherwise, just return the content (with no shortcodes)
return $content;
}
function _replace_shortcodes_notice() {
?>
<div class="updated">
<p>
All posts' content updated.
</p>
</div>
<?php
}