A background (usually an image) can be positioned very precisely with “background-position”. This property can have many values and it is not affected by the
Read moreManaging div width & height with ‘box-sizing’
Let’s say we have two divs side by side, each taking 50% of the width. We want to use padding in order to style the
Read moreAccessing database data with jQuery using a PHP proxy and avoiding the 404 error
I needed to pull database records into a page but couldn’t use PHP directly on that page (it was limited by the CMS used). So
Read morePulling latest posts from wordpress into any page
In order to pull a set number of words use:
1 2 3 4 5 6 7 8 9 10 |
<?php require($_SERVER['DOCUMENT_ROOT'].'/blog/wp-blog-header.php'); $args = array( 'numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date"); $postslist = get_posts( $args ); echo '<ul id="latest_posts">'; foreach ($postslist as $post) : setup_postdata($post); ?> <li><strong><?php the_date(); ?></strong><br /> <a href="<?php the_permalink(); ?>" title="<?php the_title();?>"> <?php the_title(); ?></a><br><?php echo apply_filters( 'the_content', wp_trim_words( strip_tags( $post->post_content ), 55 ) );?> </li> <?php endforeach; ?> </ul> |
Calling “wp-blog-header.php” is needed in order to have the wordpress functions available. If you
Read moreTagging as spam if phone field has characters in it
1 2 3 4 5 |
// This is for spam attacks that have random chars in the phone field $lwphone=strtolower($in_phone); if (strpbrk($lwphone,'abcdefghijklmnopqrstuvwxyz') !== false) { $spam='1'; } |
Error display in PHP
There are number of different types of errors in PHP . Some of the most common ones are: E_ERROR: This is a fatal runtime error.
Read moreOutputting all form fields in one recursive go
1 |
foreach ($_POST as $key => $value) echo("Field ".htmlspecialchars($key)." : ".htmlspecialchars($value)."<br>"); |
Encoding strings for curl or any type of post/get
A problem I faced was that when the string I want to pass contains “&” and goes through curl it breaks the value passed since
Read moreRemoving blank lines in Dreamweaver
1. Open the file in Dreamweaver 2. Click CTRL + F, or go to EDIT > FIND AND REPLACE 3. Select “Current document” in “Find
Read moreHTML and Plain text emails
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<?php //define the receiver of the email //define the subject of the email $subject = 'Test HTML email'; //create a boundary string. It must be unique //so we use the MD5 algorithm to generate a random hash $random_hash = md5(date('r', time())); //define the headers we want passed. Note that they are separated with \r\n //add boundary string and mime type specification $headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\""; //define the body of the message. ob_start(); //Turn on output buffering ?> --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Hello World!!! This is simple text email message. --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <h2>Hello World!</h2> <p>This is something with <b>HTML</b> formatting.</p> --PHP-alt-<?php echo $random_hash; ?>-- <? //copy current buffer contents into $message variable and delete current output buffer $message = ob_get_clean(); //send the email $mail_sent = @mail( $to, $subject, $message, $headers ); //if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" echo $mail_sent ? "Mail sent" : "Mail failed"; ?> |
Avoiding save_post to run your code twice
When attaching a function in functions.php to save_post I found out it runs my code twice. Finally found a solution using:
1 2 3 4 5 6 7 8 9 |
$post_type = get_post_type( $post_id ); if ($post_type=='revision') { return; } if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )||$_POST['post_type']=='revision') { return; } |
Making an all width element inside a set width theme
I needed to create a slider which had to be wider than the text area. There was a max-width set by the theme, and I
Read moreSecure form handling
Validate Form Data With PHP The first thing we will do is to pass all variables through PHP’s htmlspecialchars() function. When we use the htmlspecialchars()
Read moreMaking an entire div clickable / link
The simplest solution:
1 2 3 4 |
<a ..> <div> </div> </a> |
The thing is that if there are other links inside the div, then it will not work and a different solution
Read moreCSS nesting, comma, no comma, >
I have already made a post on CSS advanced techniques, but this time a short and simple overview: Using a comma between css selectors is
Read moreRenaming all file names in a directory to lowercase
Run the following in command prompt (cmd):
1 |
for /f "Tokens=*" %f in ('dir /l/b/a-d') do (rename "%f" "%f") |
Getting multiple checkbox values in PHP
If you give the checkboxes the same name, ending in [], the values are returned as an array.
1 2 |
<input type="checkbox" name="fruit[]" value="apple" /> <input type="checkbox" name="fruit[]" value="grapefruit" /> |
Then in PHP …
1 2 3 4 5 6 |
if( isset($_POST['fruit']) && is_array($_POST['fruit']) ) { foreach($_POST['fruit'] as $fruit) { echo "I have a {$fruit}!"; } $fruitList = implode(', ', $_POST['fruit']); } |
Read more
Selecting text by clicking on a button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<div id=”mails” style=”margin-top:80px”> <button name=”copy” title=”Select” >Select all</button><br/><br/> <script> jQuery.fn.selectText = function(){ var doc = document; var element = this[0]; console.log(this, element); if (doc.body.createTextRange) { var range = document.body.createTextRange(); range.moveToElementText(element); range.select(); } else if (window.getSelection) { var selection = window.getSelection(); var range = document.createRange(); range.selectNodeContents(element); selection.removeAllRanges(); selection.addRange(range); } }; $(document).ready(function(){ $(“button”).click(function(){ $(“#mails”).selectText(); }); }); </script> |
Read more
Xbmc plays audio video stays blank
After buying a new xbmc / kodi tv box (Chinese clone of mx2) I went through the entire setup, adding the various add-ons just to
Read moreMagic object functions in PHP
There are special built-in functions for objects as the constructor. They all start with “_”. one of them is _call which is called when an
Read moreDefault argument values in function declaration
A function may define C++-style default values for scalar arguments as follows: Example #3 Use of default parameters in functions
1 2 3 4 5 6 7 8 9 |
<?php function makecoffee($type = "cappuccino") { return "Making a cup of $type.\n"; } echo makecoffee(); echo makecoffee(null); echo makecoffee("espresso"); ?> |
The above example will
Read moreLoading / Submitting animation after form submission
This is done to prevent double/triple submission and notify the visitor that everything is taken care of: form – create a hidden animation div below
Read moreMedia break-points
@media (min-width: 1200px){css attributes}@media (max-width: 1199px) and (min-width: 981px){css attributes}@media (max-width: 980px) and (min-width: 641px){css attributes}@media (max-width: 640px) and (min-width: 481px){css attributes}@media (max-width: 480px) and
Read moreMaking the footer stick to the bottom of the page when there is little content
http://ryanfait.com/resources/footer-stick-to-bottom-of-page/ When logged in to wordpress the top bar for the editor messes the height of the page so it looks like the is an
Read moreAlways show next/prev arrows in NextGen gallery
This helps mobile users to know how to navigate images in the gallery. Go to the fancybox css file “jquery.fancybox-1.3.4.css” in my version. It is
Read moreScaling logo image or any image for mobile display
Do not specify image height and width. Add to image: Style=”max-width: 100%; height: auto;” That’s it
Read moreChecking if a function already exists
Declaring the same function twice will trigger an error message and stop the script. If there is an include with a function but it’s not
Read moreFile include into a variable
There is an easy way to get a file’s content into a variable: $var = file_get_contents($_SERVER[‘DOCUMENT_ROOT’].’/file.php’); Useful when building a variable that holds output (HTML)
Read moreOverlays on top of flash objects – overlapping flash
If we have a pop-up (or more accurate an overlay) on a page (a chat window, survey etc.) and the page has flash the flash
Read morePage, Post and Post List php code files
To edit, remove or style (except for the css options) titles and templates for pages and blog pages use the following files: pages: content-page.php single
Read more