Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Modify foreach loop for social sharing links?

Your issue has nothing to do with the foreach. The problem is this part:

implode( '&', $social_link['query'] )

implode() only connects the values. There’s nothing in this code that adds the keys or an =.

If you want to add an array of key/values as a query string to a URL, WordPress provides a function for this called add_query_arg(), it accepts an array of key values and a URL to add them to.

In your case you’d use it like this:

<a target="_blank" href="https://wordpress.stackexchange.com/questions/295504/<?php echo esc_url( add_query_arg( $social_link["query'], $social_link['base'] ) ); ?>">
    <?php echo $social_link['title']; ?>
</a>

Note my use of esc_url. As the documentation states:

Important: The return value of add_query_arg() is not escaped by default. Output should be late-escaped with esc_url() or similar to help prevent vulnerability to cross-site scripting (XSS) attacks.

Another thing to consider is that the values in the query string need to be URL encoded. You can do this with the urlencode() PHP function:

'title' => urlencode( get_the_title() ),

However you’ll need to do this for every value if you do it like that. A way do to this with less code would be to use array_map() to apply urlencode() to every value just before putting into add_query_arg():

foreach( $social_links as $social_link ) :
    $query_args = array_map( 'urlencode', $social_link['query'] );
    ?>

    <li>
        <a target="_blank" href="https://wordpress.stackexchange.com/questions/295504/<?php echo esc_url( add_query_arg( $query_args, $social_link["base'] ) ); ?>">
            <?php echo $social_link['title']; ?>
        </a>
    </li>

    <?php
endforeach;

Related Posts:

  1. Nice way to print_r arrays
  2. Why does get_users suddenly return duplicates?
  3. WordPress: How to use post_class() in an echo
  4. How do I pass an array as an argument to a WP-CLI command?
  5. Updating wp_options with an array on save_post results in duplicated entries
  6. Passing a variable into Contact Form 7 [closed]
  7. Comma seperated list of ID’s as an array?
  8. update_post_meta from data in multidimensional array created from a form
  9. post__in works but also prints the word Array
  10. Make Selected Mutiselect Items “selected”
  11. settings api store multiple array
  12. if is_singular array not working as expected
  13. How do I update a specific object in an array, in user meta?
  14. Get posts id in array by meta value and key
  15. How to save array of datas in option page by setting api?
  16. Sort a custom post type array numerically
  17. How to add day number and initial to my post graph?
  18. How to validate register settings array
  19. How to check an array of $curauth fields?
  20. Problem with wp_update_post
  21. Replace text inside a huge multidimensional array
  22. How to use IN array properly in WordPress?
  23. WordPress Plugin Dev: Using array for WP options
  24. Only a part of array is stored in transient – what could be causing this?
  25. update_post_meta() with a modified array?
  26. How to get update_post_meta output to include string length
  27. Return array keys and values
  28. How pass a 0 in $atts?
  29. How to save data of an input field to an array
  30. Retrieving multiple values to the Post Meta (using an array)
  31. Echoing a variable inside a function
  32. Stuck with a Custom Field Check box Array
  33. Compare with WP Meta Query
  34. How to Translate a string of an array inside a class?
  35. How to pass conditional array to wp_localize_script
  36. Combine get_page_by_title to exclude pages from wp_list_pages
  37. Plugin settings page checkbox not saving – one options array with sub array
  38. Selecting and outputting results from DB with an array
  39. Struggling with array and foreach loop
  40. Update post meta does not serialize array
  41. Customizer Settings in Arrays
  42. WordPress i18n in Array throws Error
  43. How to use json_decode a post_meta value? [duplicate]
  44. How to decipher the following array
  45. Save meta box values as an array to wp_postmeta
  46. How to display line breaked meta values in table?
  47. Organizing and grouping an array by year
  48. array_filter warning – wp-includes/post.php line 3148
  49. Hide sidebar if post_type is in array
  50. Array in meta key?
  51. Saving array keep adding length of array
  52. Obtaining values from objects
  53. How to get a page array and category array going at the same time?
  54. get_the_terms, whether I should use string or array as the second parameter?
  55. Obtaining array item from WordPress revisions output
  56. Matching slug terms from one array to those in array of WP_Term objects to output term names
  57. Print specific values stored in a post meta array
  58. Do not replicate items if they exists in a foreach loop
  59. WP Page Options Array
  60. Array is not working in Filter?
  61. How to put an array in wp user query
  62. Get the values from an array string to work with post__in
  63. return paginate_comments_links() as array
  64. Inserting a random number into an array [closed]
  65. Get array value
  66. Random element in get_post_meta array
  67. update_user_meta duplicates entry
  68. Ways of dealing with nested arrays within option
  69. Add formatting to Array
  70. How to randomise my menu items?
  71. Taxonomy terms not properly displaying as array
  72. WordPress, result array of ‘post_name’
  73. Why are array_slice() and array_chunk() not working?
  74. Retrieve array items without page ID
  75. How do i fix this “call_user_func_array()” error
  76. How can I get the content of the current page in my loop while merging post-type
  77. wp_insert_post($post), add an array of values with update_post_meta
  78. update_post_meta adding to array instead of replacing value
  79. Add Array record to Meta Post
  80. How to output get_tags array list to select box
  81. How to avoid duplicate users when I am using get_users?
  82. How to add each letter, entity, special character from post title to array
  83. Strange Behavior with update_option
  84. wp_update_post to set post IDs to drafts not working
  85. How i can extract data
  86. How to set an array of current usernames
  87. Show Tags of Custom Post Type outside of Loop
  88. “Undefined variable: array” Error In Displaying Post Tag
  89. wp_set_object_terms not accepting variable array
  90. How do I pull post from standard post format?
  91. WPML admin texts with serialized objects
  92. Merge arrays and order set and subset as one
  93. The writing ( with foreach) of the keys of an array of objects to a new array produces fatal error
  94. Create dropdown menu of all tags used in category
  95. How can I get a list of custom post IDs into a variable I can use for another function?
  96. Use $wp_customize->add_control to create a selectable “Designed/Powered By” choices select w/ link in footer?
  97. using array in wordpress database query
  98. Echo the key from a select-box in Array with get_option (Settings API)
  99. Updating wordpress user meta data array (unexpected multiple arrays)
  100. update_post_meta array issues
Categories array Tags array, get-template-part, social-sharing
Woocommerce Update Causing Internal Server Error [closed]
Force to use STRONG users password and implement rule to prevent REUSE [closed]

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress