1.01.1 Shared Functions - andperry256/my-base-theme-and-dbadmin GitHub Wiki

This section describes the functions contained within the file shared/functions.php. This file contains those functions which need to be accessed:-

  1. By scripts running outside the WordPress environment.
  2. By scripts in the wp-custom-scripts directory.
  3. By child theme scripts.

run_session()

This and the following four functions are used both inside and outside the WordPress environment. The need for these functions arose from the fact that running an active session can cause issues within WordPress. They operate by saving session variables in the global array $GlobalSessionVars and logging any subsequent updates in the database table wp_session_updates. When the session variables are loaded on loading the next page, any updates from the database are then applied. In the WordPress environment, the active session is closed once the session variables have been saved/updated.

This function must be run immediately on loading the page. Within WordPress this script is automatically loaded by the theme. The following statement must be run at the end of the main theme functions.php script:-

add_action( 'init', 'run_session', 1);

The following four functions provide an interface that acts transparently with respect to how the session variable are being accessed. In each case the $name parameter specifies the name of the session variable and can be one of the following:-

  1. A string for a simple index to the session variable in the array.
  2. An array of two strings for a two-dimensional index to the session variable in the array.

session_var_is_set($name)

This returns a boolean result to indicate if the given session variable is set.

get_session_var($name,$check=true)

This returns the value of a given session variable. The parameter $check is a boolean to indicate if the function first actively checks that the given variable is set.

update_session_var($name,$value)

This updates the value of a given session variable.

delete_session_var($name)

This deletes a give session variable.

set_default_header_image_paths()

set_header_image_paths($slug,$type)

output_page_header()

This is described under My Base Theme.

get_content_part($part_no)

This is described under My Base Theme.

output_meta_data()

This is called to set up HTML meta tags during page load. Please see Handling of Meta Data for further details.

output_stylesheet_link($path,$sub_path)

This function is called to load the stylesheet from a given point in the page/category hierarchy. The directory location is determined from the two parameters - $path is typically the base directory of the page/category hierarchy and $sub_path is therefore the appropriate sub-directory. It will always load the styles.css script from the given path/sub-path if found. Additionally it may also load styles-light.css or styles-dark.css if the 'theme_mode' session variable is set to indicate a light or dark theme.

include_inline_stylesheet($path)

This function will cause the contents of a given stylesheet to be output within the text of a page, enclosed within a set of <style></style> tags. If the 'theme_mode' session variable is set to indicate a light or dark theme, it will also look for a <filename>-light.css or <filename>-dark.css script as appropriate.

include_inline_javascript($path)

This function will cause the contents of a given JavaScript tile to be output within the text of a page, enclosed within a set of <script></script> tags.

save_php_error_log()

[---Description goes here---]

restore_php_error_log()

[---Description goes here---]

output_to_access_log($user='',$add_info='')

[---Description goes here---]

readable_markup($str)

This function will process a piece of text to make it more readable in a web browser by converting:-

  1. '<' to '&lt;'
  2. '>' to '&gt;'
  3. newline to '<br/>'+newline

simpify_html($content)

This function is called to simplify a word processed document (from Word, LibreOffice Writer etc) that has been saved/exported as HTML. Its main purpose is to strip out formatting information that would otherwise be handled by CSS. This function is in early stages of development and may need refinement once it starts to be used. The following array is defined within the function:-

$allowed_tags = array('<p>','<br>','<a>','<table>','<th>','<tr>','<td>','<ul>','<ol>','<li>','<b>','<i>','<u>');

This may be overridden by declaring a global version of the same array. It specifies the HTML tags that are to be retained within the document, though all tag attributes are stripped out.

There is also a function:-

simpify_html_tag($content,$tag);

The parameter $tag specifies the the tag to be stripped out (without the angle brackets). It is called to remove a given tag type by a long-hand method if there are issues with using the standard PHP strip_tags function. It is currently called by simpify_html in connection with the <style> tag.

⚠️ **GitHub.com Fallback** ⚠️