Skip to content

Commit dcdbab5

Browse files
committed
inital commit
0 parents  commit dcdbab5

File tree

8 files changed

+150
-0
lines changed

8 files changed

+150
-0
lines changed

Readme.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# WordPress Fns
2+
3+
A collection of functions to help with WordPress theme & plugin development.
4+
5+
# Install
6+
7+
Install from Composer:
8+
9+
```bash
10+
composer require ed-itsolutions/wordpress-fns
11+
```
12+
13+
Then add `require('vendor/autoload.php');` to the top of your `functions.php`.
14+
15+
# Functions
16+
17+
## wordpress_fns_as_filter($functionName, $priority = 10, $args = 1)
18+
19+
Adds the named function `$functionName` as a filter with the same name.
20+
21+
## wordpress_fns_featured_image_at_size($postId, $imageSize)
22+
23+
Returns the given posts `$postId` featured image at size `$imageSize`.
24+
25+
Returns `false` if the post does not have a featured image.
26+
27+
## wordpress_fns_get_attachment_id_from_url($url)
28+
29+
Returns the id of the attachment from the url. Extremely useful when working with meta boxes etc... that store an images URL instead of its attachment id.
30+
31+
Returns `false` if no attachment could be found.
32+
33+
## wordpress_fns_get_page_sub_menu_items($pageId, $menuName)
34+
35+
Returns an array of menu items that are the direct desendants of the supplied page `$pageId` on the menu `$menuName`.
36+
37+
Returns an empty array if it couldn't find the menu item for the supplied page.
38+
39+
## wordpress_fns_header_or_featured_image($imageSize)
40+
41+
If the page has a featured image that image at the given size `$imageSize` will be returned, otherwise it returns the header image set in the customizer.
42+
43+
# Contributing
44+
45+
We welcome PRs to this project. If you have a function or improvement feel free to add them.
46+
47+
We ask that:
48+
49+
- All functions be prefixed with `wordpress_fns_`.
50+
- All functions return `false` instead of throwing an error or null.
51+
- Don't add actions or filters, if users want to use a function as a filter they can do it themselves.

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "ed-itsolutions/wordpress-fns",
3+
"description": "Functions that make working with WordPress easier.",
4+
"require": {
5+
"php": "^5.3.3 || ^7.0"
6+
},
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Adam Laycock",
11+
"email": "[email protected]",
12+
"homepage": "https://arcath.net",
13+
"role": "Developer"
14+
}
15+
],
16+
"autoload": {
17+
"files": ["wordpress-fns.php"]
18+
}
19+
}

fns/as_filter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
function wordpress_fns_as_filter($functionName, $priority = 10, $args = 1){
3+
add_filter($functionName, $functionName, $priority, $args);
4+
}

fns/featured_image_at_size.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
function wordpress_fns_featured_image_at_size($pageId, $size){
3+
$attachmentURLS = wp_get_attachment_image_src(get_post_thumbnail_id($pageId), $size);
4+
$imageUrl = $attachmentURLS[0];
5+
6+
if($imageUrl){
7+
return $imageUrl;
8+
}
9+
10+
return false;
11+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
function wordpress_fns_get_attachment_id_from_url($url){
3+
global $wpdb;
4+
5+
// Get the attachment ids from the database
6+
$attachment = $wpdb->get_col(
7+
$wpdb->prepare(
8+
"SELECT ID FROM $wpdb->posts WHERE guid='%s';",
9+
$url
10+
)
11+
);
12+
13+
// If found return the attacment
14+
if(count($attachment) != 0){
15+
return $attachment[0];
16+
}else{
17+
// Return false if not found.
18+
return false;
19+
}
20+
}

fns/get_page_sub_menu_items.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
function wordpress_fns_get_page_sub_menu_items($menuName, $pageId){
3+
$locations = get_nav_menu_locations();
4+
$menu = wp_get_nav_menu_object($locations[$menuName]);
5+
$allMenuItems = wp_get_nav_menu_items($menu);
6+
$menuId = 0;
7+
8+
// Return an empty array if no menu items could be found.
9+
if(empty($allMenuItems)){
10+
return array();
11+
}
12+
13+
// Find the menu object ID for the current page.
14+
foreach($allMenuItems as $item){
15+
if($item->object_id == $pageId){
16+
$menuId = $item->ID;
17+
}
18+
}
19+
20+
$items = array();
21+
22+
foreach($allMenuItems as $item){
23+
if($item->menu_item_parent == $menuId){
24+
$items[] = $item;
25+
}
26+
}
27+
28+
return $items;
29+
}

fns/header_or_featured_image.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
function wordpress_fns_header_or_featured_image($imageSize){
3+
if(get_the_post_thumbnail(null, $size = $imageSize)){
4+
$attachmentImageSrc = wp_get_attachment_image_src(get_post_thumbnail_id(), $imageSize);
5+
return $attachmentImageSrc[0];
6+
}
7+
8+
return get_header_image();
9+
}

wordpress-fns.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
require_once('./fns/as_filter');
4+
require_once('./fns/featured_image_size.php');
5+
require_once('./fns/get_attachement_id_from_url.php');
6+
require_once('./fns/get_page_sub_menu_items.php');
7+
require_once('./fns/header_or_featured_image.php');

0 commit comments

Comments
 (0)