Articles of php

How can I output a php value into a JS file within WordPress?

I am trying to use get_theme_mod option from php to load into my js file. I’ve used wp_localize_script to convert. The tweet_id is where I need to place the $tweet value outputted. Any help would be great! (PS: I’ve registered the script in functions.php already.) PHP FILE <? $tweet = get_theme_mod( ‘tweets_widget_id’ ); wp_localize_script( ‘twitter’, […]

How to display php source code inside a post using visual editor?

NOTE I don’t want the php code to be executed, only displayed. I want to display this php code <?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?> inside a post. I cannot make it work. If I go to text type of editor and type <pre class=”lang:php nums:false”> <?php wp_enqueue_script( $handle, $src, $deps, $ver, […]

spliting posts into two columns

This is a continuing question from Merging a complex query with post_rewind and splitting posts into two columns Hi, I need help with my loop. What is does is split posts into two columns. <?php $row_start = 1; while ( $query->have_posts() ) : $query->the_post(); ?> <?php if ( in_array( get_the_ID(), $duplicates ) ) continue; ?> […]

action hook wp_head higher priority as all other plugins/hooks

I work a bit with the wordpress API and I want to try to include a <meta>-tag into my head area of my website. This is my code (works everything fine!): class dmd_noindex_options_page{ function __construct() { add_action( ‘wp_head’, array( $this, ‘set_meta’) ); } function set_meta(){ $pages = get_option(‘dmd_noindex_pages’); $pages_id = explode(‘,’, $pages); for($i = 0; […]

Can I use HTTP POSTs? Is there a better alternative?

I am use to php development in general and I am use to getting a lot done by making a POST to a php file. However I have not seen people using $_POST in WordPress. I have seen one discussion about someone wanting to create new posts on their blog using $_POST, and they were […]

Accessing a random image via ajax

The goal with this is to create a random banner, managed in the wp admin. I can create a banner solution in the wp admin without an issue but need a little help on the ajax side. I have an image http://example.com/wp-content/uploads/2015/12/image-name.jpg I created a random_banner function in functions.php. add_action( ‘wp_ajax_random_banner’, ‘random_banner’ ); add_action( ‘wp_ajax_nopriv_random_banner’, […]

Get the title before comma

I am using this code to return the title of the post on a gravity form field. add_filter(‘gform_field_value_page_name’, ‘page_name_filler’); function page_name_filler($value){ return get_the_title() ; } However, usually the title of the post is too long and it overfills the field. Hence I would only like to retain the first portion of the title before a […]

Create a random unique 6 digit number as custom field for custom post type

I use the following function to create a random unique 6 digit number and save it in a custom post type meta field. I am unsure about the use of rewind_posts() inside a while loop. The idea is that it checks if the $random number already exists inside any of the existing custom posts. If […]

How to output wordpress custom tags separated by comma?

Based on a few references I’ve built a function to output a list of custom taxonomy terms separated by comma. The code works as intended, where food_tag is the custom taxonomy I registered for a custom post_type. Here’s the function: function get_taxonomy_food_tags(){ $terms = get_terms(‘food_tag’); foreach($terms as $term){ // The $term is an object, so […]

Differences when using the the_time and the_date functions

I have some doubts about how work the WordPress the_date() functions definied into general-template.php In a page I show the posts archive using this code snippet into a loop: <article id=”post-<?php the_ID(); ?>” <?php post_class(); ?>> <header> <h3 class=”entry-title”> <a href=”<?php the_permalink(); ?>” title=”<?php printf(__(‘Permalink to %s’, ‘your-theme’), the_title_attribute(‘echo=0’)); ?>” rel=”bookmark”><?php the_date(“d/m/Y”); echo ” – […]