I have the following code which is inside its own file, which gets an ACF field and outputs it.
<?=get_field('text') ?>
I then include this in another template file using PHP include, meaning I have re-usable fields throughout my site. This has worked well in the past, as I can create consistent 'text' fields such as: <h1>My Text field</h1>
However, I'd like to further extend this by writing some kind of fucntion that allows me to call the PHP include, whilst also assigning the container tag (h1,h2 etc), as well as optional classes:
<h1>My Text field</h1>
<h2>My Text field</h2>
<p>My Text field</p>
<h1 class="myClass">My Text field</h1>
Is this possible?
You can do it this way:
<?php
class functions{
public static function get_field($text = 'Default', $tag = 'p', $class = false){
if($class){
$class = ' class="' . $class . '"';
}
return '<' . $tag . $class . '>' . $text . '</' . $tag . '>';
}
}
echo functions::get_field('My Text', 'h1', 'my-class');
?>
in HTML
<h1 class="my-class">My Text</h1>
Hope it will give you an idea on how to expand this method.
Related
My Wordpress plugin creates a few shortcodes that return blocks of HTML.
When I register the shortcodes, I do so like this:
add_shortcode('bb-loans-form', function() {
return Shortcodes::loanApplicationForm();
});
And here is the static method from the Shortcodes class:
public static function loadApplicationForm()
{
$form = new \AdamWathan\Form\FormBuilder;
$html = $form->open()->action('/apply')->class('bb-loan-form');
$html .= '<div class="bb-form-field">';
$html .= '<h2>Loan Application Number</h2>';
$html .= $form->text('loan_app_number')->id('loan-app-number');
$html .= $form->submit('Continue Loan');
$html .= '</div>';
$html .= $form->close();
return $html;
}
This is very cumbersome, and messy. I don't like outputting the HTML like this. I've also used Heredoc, but I had to use string substitution to include important values when the form is rendered.
Is there a better way to store my HTML files? I don't want these files publicly accessible. They would have to live in my plugin directory.
It's not a huge plugin, so I'm not overly concerned, but I'd like to know for future reference if there's a cleaner way to include the needed HTML.
You could just use a single string with concatenations...
$form = new \AdamWathan\Form\FormBuilder;
$html = $form->open()->action('/apply')->class('bb-loan-form') .
'<div class="bb-form-field">
<h2>Loan Application Number</h2>' .
$form->text('loan_app_number')->id('loan-app-number') .
$form->submit('Continue Loan') .
'</div>' .
$form->close();
return $html;
It at least keeps the HTML aligned.
I also don't really see an issue with Heredoc, as long as you assign variables and substitute them in:
$form = new \AdamWathan\Form\FormBuilder;
$form_start = $form->open()->action('/apply')->class('bb-loan-form');
$loan_app = $form->text('loan_app_number')->id('loan-app-number');
$submit = $form->submit('Continue Loan');
$form_end = $form->close();
$html = <<<HTML
{$form_start}
<div class="bb-form-field">
<h2>Loan Application Number</h2>
{$loan_app}
{$submit}
</div>
{$form_end}
HTML;
return $html;
Run into a bit of a sticky situation which I can't seem to wrap my finger around. Basically what I am trying to achieve is having the ability to inject different Javascript files on different page.
Some simple, random example:
Page 1: import jquery.js
Page 2: import mootools.js
So what I have done is, I've created a function called addScript() like so:
function addScript($file) {
$script = '';
$script .= '<script src="'. REL_PATH . '/path/to/file/' . $file . '">';
$script .= '</script>';
return $script;
}
so if I call addScript('jquery.min'); it, outputs correctly.
What I now want to do is replace the closing </head> tag with the output from the above function. If I do the following then it works fine:
ob_start();
require_once("models/header.php");
$contents = ob_get_contents();
ob_end_clean();
echo str_replace('</head>', addScript('jquery.js') . '</head>', $contents);
However I would like this to be a little more dynamic as there may be multiple script that I need to inject on each page like so:
addScript('script.js');
addScript('script2.js');
addScript('script3.js');
I then thought of creating a getHead() function with a foreach loop inside and returning str_replace there instead but this did not work.
Can anyone guide my in the direction to dynamically inject as many script as required and output the last bit of the head?
Why not do something like this:
class Assets {
private static $css = array();
private static $js = array();
static function add_style($path) {
self::$css[] = $path;
}
static function add_script($path) {
self::$js[] = $path;
}
static function get_styles() {
$output = '';
foreach(self::$css as $path) {
$ouput .= '<link rel="stylesheet" href="'. $path .'" />' . "\n";
}
return $ouput;
}
static function get_scripts() {
$output = '';
foreach(self::$js as $path) {
$ouput .= '<script type="text/javascript" src="'. $path .'"></script>' . "\n";
}
return $ouput;
}
}
Then anywhere in your project:
Assets::add_style('path/to/style.css');
Assets::add_script('path/to/jquery.js');
And in header.php:
<head>
<!-- other header stuff -->
<?php echo Assets::get_styles(); ?>
<?php echo Assets::get_scripts(); ?>
</head>
Is much more convenient, and you can can extend the class to do more fancy stuff.
Disclaimer: there is much debate about using static vars, as they look like globals. I agree, but this is quick-and-dirty and works no matter what kind of framework you use. You can also make the variables oldschool instance vars, but then you'll have to pass the assets object to the header.php as well.
What's wrong with the following??
echo str_replace('</head>',
addScript('jquery.js').
addScript('jquer1.js').
addScript('jquer2.js').
addScript('jquer3.js').
'</head>', $contents);
How about you put the ob_start(); in header.php. Then your function is:
function addScript($file) {
$script = '<script src="'. REL_PATH . '/path/to/file/' . $file . '"></script>';
echo str_replace('</head>', addScript('jquery.js') . '</head>', ob_get_clean());
}
Then:
addScript('script.js');
This method keeps the output buffer going and you can manipulate it later in the script whenever you want. just as you do with the addScript().
Im creating a custom function for my wordpress website that will add a review section below the post content and i need to insert this function from another another file into a custom function that i added to my functions.php. I need to get this piece of code $buffy .= $this->get_review(); from a different file to work in this function:
function content_injector($content) {
global $wp_query;
$postid = $wp_query->post->ID;
if (is_single((get_post_meta($postid, 'top_ad', true) != 'off' ))) {
$top_ad = do_shortcode('[td_ad_box spot_name="Ad spot -- topad"]');
}
if (is_single((get_post_meta($postid, 'bottom_ad', true) != 'off' ))) {
$bottom_ad = do_shortcode('[td_ad_box spot_name="Ad spot -- topad"]');
}
if (is_single()) {
$review = //HOW DO I ADD THAT get_review CODE HERE?
$custom_share = '<div id="title-deel"><h4 class="block-title"><span>DEEL</span></h4></div>' . do_shortcode('[ssba]');
$facebook_comments = '<div id="title-reageer"><h4 class="block-title"><span>REAGEER</span></h4></div>' . '<div class="fb-comments" data-href="' . get_permalink() . '" data-colorscheme="light" data-numposts="5" data-mobile="false" data-width="700"></div>';
}
$content = $top_ad . $content . $bottom_ad . $custom_share . $facebook_comments;
return $content;
}
add_filter('the_content', 'content_injector');
As you can see i need to add the get_review function to $review, but i cant make it work on my own. How to make this work?
Use include to include the file before using any methods from that file.
include("file_with_functions.php");
OR
Create a class (with filename same as classname).
Include the file.
Create an instance of the class.
Access the method in the class through the object
(Preamble: Am new to PHP, coming from a C# background where I am used to very clean code. Am currently working on my own Wordpress site which has a purchased theme.)
I have seen this type of code in a WordPress theme:
<img src="<?php echo esc_url( $logo ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name' ) ); ?>" id="logo"/>
I find this very hard to read compared to the refactored:
<?php
echo '<a href="';
echo esc_url( home_url( '/' ) );
echo "><img src=";
echo esc_url( $logo );
echo " alt=";
echo esc_attr( get_bloginfo( 'name' ) );
echo '" id="logo"/></a>'
?>
But this is the easiest by far:
<?php
get_anchor($url, $imgsource, $alt, $id);
?>
get_anchor being a custom function that echos an anchor configured according to the parameters.
But surely I am not the first to think of this. Are there any existing libs that have a set of functions that return properly formatted html like in this example? Is there something I am missing?
I've written a function that returns a HTML tag based on the pure PHP output:
function tag($name, $attrs, $content) {
$res = '';
$res .= '<' . $name;
foreach($attrs as $key => $val)
$res .= ' ' . $key . '="' . $val . '"';
$res .= isset($content) ? '>' . $content . '</'.$name.'>' : ' />';
return $res;
}
$name is the tagname (e.g. a)
$attrs is a key, value array with attributes (e.g. array('href','http://google.com/'))
$content is the content / body of the tag (an other element or text)
Example basic use:
echo tag('a', array('href' => 'http://google.com/'),'Google');
Example nested use with multiple children:
echo tag('ul',array(),
tag('li',array(),'one') .
tag('li',array(),'two') .
tag('li',array(),'three')
);
I believe what you are looking for are templates like Smarty. They are the cleanest way to display information as code and view are completely separated.
However Wordpress do not use them, I don't know why actually, probably because most PHP programmers are not used to it.
Most of the PHP frameworks provide such libraries to out put html through parameterized functions, most of them are part of view layer if the framework follows MVC pattern.
but if you are not using any of the framework then you may use these libraries from here
PHP Pear Packages
And for building forms in particular see
HTML_QuickForm2
I'm trying to build a placeholder meta description for a page, in case the user hasn't included a description in the CMS.
I have started with the following code, but of course it fails if any of the other variables are empty too, such as $phone, $location['zip'] and so on.
<?php
if (!empty($description)) {
echo '<meta name="description" content="' .$description . '">';
}
else {
// Should return: Apple is a business located in Palo Alto, 95014. Call 408.996.1010...
$description = $name . ' is a ' . strtolower($category) . ' located in ' . $location['city'] . ', ';
$description .= $location['zip'] . '. Call ' . $phone . ' for more details today.';
echo '<meta name="description" content="' . $description . '">';
} ?>
What's the most efficient way to build a description in this way? Currently I can only think of nested if statements which sounds messy and I'm sure there must be a clean way to do this.
add a function to check if value is set?
i.e.
function checkData($data) {
if(!empty($data)) {
return $data;
} else {
return '';
}
}
$description = checkData($name) . ' is a ' . strtolower(checkData($category)) . ' located in ' . checkData($location['city']) . ', ';
As the description is something that varies based on the input, put it into a function or class of it's own to encapsulate it:
/**
* build a description based on various input variables
* #return string
*/
function build_description($description, $name, $category, array location, $phone) {
// build the description as you see fit.
}
$description = build_description(compact('description', 'name', 'category'));
$metaDescription = sprintf('<meta name="description" content="%s"', htmlspecialchars($description));
that done, the concrete implementation within build_description can contain a lot of complex statements, while the rest of the program can deal with it as if it is something simple.
However, this does not answer how you could code it inside that function. But as the data of the output of that function heavily depends on the input of that function, you can only deal with all aspects the arguments do impose.
The other variables defined shouldn't be string but part of an object such as... Description.
In this case, it would be easier, calling a Description->isEmpty() that returns true if one of those variables are empty.
If you are stuck with this configuration, you still can make an array:
$myArray=array($name, $category,...);
and check in a loop or maybe the return of in_array('',$myArray)