Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts
Monday, July 10, 2017
WordPress Intro Notes
Tips
Change Permalinks in Settings to use non date format unless just blog. Can change per page.
Design:
Custom Pages
Page Templates and Template Partial (part of a page)
Plug-in Page
Error Page
WordPress Theme Structure
Style.css should be in the location of theme .html files
functions.php
Custom Files
These are default templates that are used when a more specific template can't be found
Page Templates
index.php is the only required template file... most generic...should be flexible
page.php is used to display pages
home.php is used for the front page and shows latest posts
front-page.php is used for static front pages
Posts Templates
single.php is for individual post
archive.php is for post archives
category.php is for posts of a specific category
tag.php is for posts of a specific tags
Template Partials
a section or part of a web page that is encapsulated in a template that can be pulled in to any page.
comments.php
sidebar.php
footer.php
header.php
search.php
attachment.php
date.php
image.php
404.php
author.php
rtl.php
taxonomy.php
Can combine them
i.e.
Archive-cat.php
Page-contact.php
Single-lady.php
Template Hierarchy
Determines how WordPress chooses a template file to display a page.
There is a flowchart of wordpress.org
Development
wp-config.php
Define('WP_DEBUG', true); // for development only
everything outside of wp-content folder is overwritten on update
themes are at wp-content/themes/mythemehere
create a directory here as shown above.
create the two required files for a theme.
style.css
index.php
In order for WordPress to recognize our theme we have to add the following to the style.css file.
The basic is:
/*
Theme Name: Some user friendly name for my theme here
Author: me
Description: Custom theme created for my company
textdomain: mytheme
*/
At this point it will show under Themes in WordPress.
Can also copy it from an existing theme such as Twenty Sixteen. It is just a comment at the top of the style.css file
that says the name of the theme, etc.
Notice there is no image showing a preview of our theme. Can add a Screenshot.png in the mythemehere directory. It can't be larger than 1200 x 900.
To move over my css, Copy my css into the style.css file. If using something like bootstrap that has its own css files use wp_register_style and wp_enqueue_style and add_action. Or more simply: <link rel="stylesheet" type="text/css" href="<?php echo get_template_directory_uri(); ?>/style1.css" />
This should also work: <link href="<?php echo get_stylesheet_directory_uri().?>'/style2.css'; ?>" rel="stylesheet" /> and appears to be built for purpose.
A template should not include the stuff in the header or footer. That would be in the header and footer templates.
A simple example for index.php would be
<?php get_header(); ?>
<div>some html</div>
<?php get_footer(); ?>
In the header.php include all starting with <!DOCTYPE html><html>. Add <?php wp_head(); ?> just before the end of the </head> tag so php can inject their own stuff. Delete title tag so WordPress can do it.
In the footer.php include all starting with footer text and the </body><html>. Add <?php wp_footer(); ?> just before the end of the </body> tag so php can inject their own stuff.
When referencing an image from the front-page.php file (for example) use.
If we want want to create a template for a specific page (not the front page) then use name the file something like page-contact.php such that page-<slug>.php. The template will show under the Page Attributes when adding the page. In this scenario all the page specific html and content can be in this file since it will only ever be used by this page. Effectively, the header and footer need to be removed and replace with the php calls to pull in the footer.php and header.php files we defined. Also any paths to images, etc will need to be prefixed with <?php bloginfo('template_url')?>. The downside of putting the content in it will be that it is not editable from WordPress and the theme files itself would need to be updated. BTW, to find what the slug, edit the page and go to Screen Options and check Slug. The slug will now show on the page.
<img src="<?php bloginfo('template_url') ?>/images/myimage.jpg">
If in style.css it would just be images/myimage.jpg assuming images/myimage.jpg and the style.css are in the same directory in my theme directory.
We can also create one for all pages (global). In this case name the file page_full-width.php for example where full-width is not a slug for any page.
To add menus to the header we can use WordPress's menu that can be edited using WordPress's tooling. Just add <?php wp_nav_menu( $args = array ('menu_class' => 'my-nav-class', 'theme_location' => 'primary')); ?> where the menu is to be used
To have the navigation also show in the footer.php add
<?php wp_nav_menu( $args = array ('menu_class' => 'my-footer-nav-class', 'theme_location' => 'footer')); ?>
Notice the Menus or Widgets isn't showing under Appearance menu in WordPress. We need to add the functions.php to file to our theme.
NOTE: there are no namespaces so function names can conflict with other vendor's functions. Best practice is to include the name of theme in function name.
Add functions.php to theme.
A basic file would look like:
<?php
if (! function_exists('mytheme_setup')) :
function mytheme_setup() {
add_theme_support('title-tag');
}
endif;
add_action('after_setup_theme', 'mytheme_setup()');
/* Register Menus */
function register_mytheme_menus() {
register_nav_menus(
array(
'primary' => __('Primary Menu'),
'footer' => __('Footer Menu')
)
);
}
add_action('init', 'register_mytheme_menus');
/* Add Stylesheets, fonts, etc */
function mytheme_scripts() {
wp_enqueue_style('mytheme_styles', get_stylesheet_uri());
wp_enqueue_style('mythemem_google_fonts', 'http://fonts.googleapis.com/css...');
}
add_action('wp_enqueue_scripts', 'mytheme_scripts');
Now we can remove those stylesheets and fonts from the header.php.
Now Menus is showing under Appearance in WordPress. Now we can use the Menu editor in WordPress to create our menus. Notice WordPress knows about Primary Menu adn Footer Menu as theme locations.
Widget Areas allow the content to be changed in WordPress by changing a widget. In functions.php we need to register it.
/* Add Widget Areas */
function mytheme_widget_init() {
register_sidebar( array ('name' => __('Main Sidebar', 'mytheme'),
'id' => 'main-sidebar',
'description' => __('Some descr', 'mytheme'),
'before_widget' => '<section id="%1$s" class="%2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>'
));
}
add_action('widgets_init', 'mytheme_widget_init');
Add a file called sidebar.php
In the file add something like
<?php if (is_active_sidebar('main-sidebar')) : ?>
<aside class="sidebar widget-area">
<?php dynamic_sidebar('main-sidebar'); ?>
</aside>
<?php endif; ?>
Now Widgets will show up under Appearance in WordPress. Any of WordPress's widgets can be added to the area now. This includes the Text/HTML editor to allow for custom content each time we use the page template
Can use Custom Post type to allow users to use WordPress to edit content using form. Like a data driven template. These custom types show up in menu and edited like a database.
Security Concerns:
Brute force login
Don't use admin as username
Use secure password
Enable two-factor authentication
.htaccess to blog certain ip addresses
Comment Spam
Keep an eye on your comments
Tweak Discussion settings to limit links and blacklist keyowrds or ip addresses
Require users to register in order to make comment
Plug-ins
Security Plugins
All in One WordPress Security and Firewall Plugin
iThemes Security
Securi Security
Top Backup Plugins
BackUpWordPress
BackupBuddy
Top SEO Plugins
WordPress SEO by Yoast
Other
Broken Link Checker
Google Analytics
W3 Total Cache
Sync - for managing multiple WordPress sites
Wednesday, March 5, 2008
PHP MS SQL extension connection bug
I found out the hard way that when I installed PHP Eclipse and select MS SQL plugin in the installer, it installs ntwdblib.dll in the same directory as php.exe. This is fine, except that the version that it installs is BUGGY! It doesn't seem to support anything but using Windows Authentication when I try to connect to the database. There is an option in the php.ini that is support to let you change this so that it will use SQL Server logins, but it doesn't use the option, it simply ignores it! When trying to connect to a database using something like:
$c = @mssql_connect("mydbserver","sqlUserHere","password", true)
It says it cannot connect when I run it from IIS which uses anonymous user. It does work if I use
php.exe -f test.php
since this process runs as me, and I have access to the database.
That is the general idea of the problem.
Here is what I found.
The version of ntwdblib.dll that is installed by the PHP installer is 2000.2.8.0.
The version you need to fix this bug is: 2000.80.194.0.
As it turns out MANY people have had the same problem. Read the numerous posts on this here.
This is a good page to use for all problems with the MS SQL PHP extension.
I hope this helps.
Brent
Monday, March 3, 2008
Installing PHP on Windows
PHP is in many ways like ASP or JSP. In this blog entry I will share some of the things I ran into while installing PHP on Windows and IIS 5 (IIS that comes with Windows XP Professional).
First download the software:
http://us2.php.net/get/php-5.2.5-Win32.zip/from/a/mirror/php-5.2.5-Win32.zip
Run the installer. I recommend installing PHP at C:\PHP instead of C:\Program Files\PHP. Do NOT select all extensions as some of them have other prerequisites that can make your installation not work. Installing more extensions than needed complicates troubleshooting. You can always to go Add/Remove Programs again and add more extensions after you know the basic installation works fine.
After you run the installer, there are some things you may need to do to get things to work properly.
You will want to set some environment variables in Windows.
1. Append C:\PHP; to the Path Windows Environment Variable.
2. Create a new Windows Environment Variable called PHPRC and set it to c:\PHP\php.ini assuming you installed PHP at c:\PHP. This will tell Windows where the php.ini file is should it get confused. You can verify that PHP knows were your php.ini file is by using the test page shown later in this blog. On the test page, verify that the line labeled "Loaded Configuration File" should be c:\PHP\php.ini. You will probably need to restart IIS for it to see the new environment variable.
You will want to make sure that PHP is properly configured in IIS.
Get Properties on Start Menu | Admin Tools | Internet Information Services | <machine> | Web Sites.
On the Home Directory tab, click the Configuration button.
In the Application Configuration dialog click the line with .php as the extension, then click the Edit button.
On the "Add/Edit Application Extension Mapping" make sure the fields have the values: .php, C:\PHP\php5isapi.dll, All Verbs, Script Engine checkbox is checked, and Check that files exists checkbox is checked. The installer will put a DOS style path instead of C:\PHP\php5isapi.dll. This is probably ok, but if you have problems try the full win32 path as noted here.
To verify that the installation was successful, all you have to do is use your favorite text editor to create a .php file and access it. I recommend the contents of the file be the following as it will prove several things:
1. That IIS is handing off the request to Php5isapi.dll
2. That PHP tags are being interpreted properly by PHP
3. Give you lots of information related to how PHP is configured.
<html><head><title>Test PHP Page</title></head>
<body>
Success for PHP
<?php
echo "Hello PHP Scripting";
?>
</body>
</html>
If you have any trouble I suggest that you read this article on how to manually install PHP. It will give you a better understanding of what the installer does, and how PHP works.
http://www.iis-aid.com/articles/how_to_guides/installing_php_5_on_iis_in_5_simple_steps?page=0%2C0
For more information on doing development in an IDE with a debugger, check out my other blog entry titled "PHP Development Environment."
PHP Development Environment
Let me first start by saying that I have only started using PHP because I have to maintain a project that was written in PHP, so my experience is quite limited. However, I figure many others may be in the same situation. At the very least I wanted to note all the things I had to do to get my project to work.
There are some very basic things I expect when doing development.
1. An IDE
2. A debugger that is integrated into the IDE
3. Stability in the IDE and debugger.
This was not as easy as I thought it would be. However, after reading some blogs, etc, I determined there is at least one free option that actually work pretty well.
Please read my comments below to better understand why I chose the tools I chose.
The IDE is most important to me. Beyond the requirements above, I need it to be free because the project I am working on doesn't have a budget to buy us a pricey IDE.
The IDE I like is PHP Development Tools (PDT), which is basically an Eclipse plug-in that allows you to do PHP development (including debugging) in the Eclipse IDE. Eclipse is a stable IDE with lots of features and is the same as what I use for Java development so the learning curve is smaller.
I also tried Maguma Studio IDE and found it to be EXTREMELY frustrating. My scroll wheel crashes the IDE every time I touch it, and I do that a lot without even thinking about it. It also didn't seem to support virtual directories, so my projects had to be in my web root, which was not what I like to do. The debugger required regularly changing my php.ini if I remember correctly, which was more work than I felt I should have to do. If you those things don't bother you, you may like this product.
There are a couple of debuggers out there, but I recommend the Zend debugger as it seems to work well with complex projects that have nested directories of source code. It
Debuggers for PHP come in different flavors and are compatible with different products and different versions of products. I found this VERY confusing and a hassle to get my IDE and debugger working together. For this reason, I STRONGLY recommend that you use an all in one download / installation. This more or less guarantees the IDE and debugger will work together and with any luck even be pre-configured which can save you time.
I believe the debugger choices are:
1. Zend Debugger
2. XDebug
3. DBG
I have not personally used anything except Zend Debugger, but my co-worker has, and he had trouble with nested directories and just generally getting it to work.
I like the Zend Debugger for several reasons.
1. I can download an all-in-one that includes the Zend and XDebug debuggers) and it fast to download (the site is fast)
2. It is comes pre-configured
3. It seems to work on nested directory structure
4. Seems stable
5. It works with the latest version (3.3.1.1) of Eclipse IDE
6. Use the newest PDT 1.0.2 (R2008010)
OK, now that you understand a little more of what I chose and why, here are the links to the downloads.
All-in-one download that includes Zend and XDebug debuggers, Eclipse IDE 3.3.1.1, PDT 1.0.2 (R20080103)
http://downloads.zend.com/pdt/all-in-one/dt-1.0.2.R20080103_debugger-5.2.12.v20071210-all-in-one-win32.zip
If you don't want / need the Zend debugger I have heard good things about the PDT all by itself, but the site is much slower, and you won't get the Zend debugger, but it is the best place to get the latest PDT (I think)
http://download.eclipse.org/tools/pdt/downloads/release.php?release=R20080103
You will also need the Java Runtime (JRE 5) to run the IDE. The JDK should also work if you already have it installed.
http://java.sun.com/javase/downloads/index_jdk5.jsp
PHP (I don't actually recommend you install PHP separately since PDT comes with it. There are some things you will want to do if you want to run your project without the IDE on your machine. See http://www.nusphere.com/kb/phpmanual/install.windows.iis.htm).
Download PHP Separately (not recommended) http://us2.php.net/get/php-5.2.5-Win32.zip/from/a/mirror/php-5.2.5-Win32.zip
A few tips:
You will want to set the .php extension to point to something like:
"C:\Program Files\pdt-1.0.2.R20080103_debugger-5.2.12.v20071210-all-in-one-win32\eclipse\plugins\org.zend.php.debug.debugger.win32.x86_5.2.12.v20071210\resources\php5\php-cgi.exe"
You can do this at the Default Web Site level. However, in IIS those changes are COPIED when you create a virtual directory to your project (assuming you use a virtual directory and not just put your project in the web root itself). This means that If you set the .php extension BEFORE you create the virtual directory it will COPY that extension to the virtual directory extensions. However, if you set the .php extension AFTER you create the virtual directory, you will need to also put it in the extensions for that virtual directory.
Subscribe to:
Posts (Atom)