OG Gallery Custom Links developer's guide¶
This page lists the hooks, REST routes, data and scripts in OG Gallery Custom Links. For the settings and everyday use, see the user guide.
Filters¶
| Filter | Arguments | When it runs |
|---|---|---|
gallery_custom_links_enabled |
bool $enabled (default true) |
On every front-end page, before the page is parsed. Return false to skip the page. |
gallery_custom_links_classes |
array $classes (default ['']) |
Before parsing. Each entry is a CSS selector prefix; the plugin looks for {selector} img. The default empty entry matches every image. The plugin adds .oggl-gallery when OG Gallery is active. |
oggcl_link_url |
string $url, int $media_id |
Each time the link of an image is read: for HtmlDomParser and DiDom, for the Javascript engine's REST route, and for OG Gallery link attributes. Return a different URL to change where the image links. Per Page Linkage uses this filter. |
oggcl_linkers |
bool $handled, $element, $parent, int $media_id, string $url, string $rel, string $aria, string $target |
For each linked image with HtmlDomParser or DiDom, before the built-in linker runs. Return true if you wrote the link yourself. $element and $parent are parser nodes (HtmlDomParser node or DiDom\Element). |
oggcl_button_linker |
string $html, string $class_names, string $url, string $label, string $rel, string $target |
Javascript engine only, when Use Buttons is on. Return the button HTML for an image whose parent and grandparent classes are $class_names. |
oggcl_allow_setup |
bool $allowed (default: manage_options) |
Permission check for the settings REST routes. |
oggcl_allow_usage |
bool $allowed (default: the administrator role) |
Permission check for the link-editing REST route and the Media Library column. |
The plugin also hooks into OG Gallery's oggl_link_attributes filter, so OG Gallery builds the link itself (href, target, rel) for linked images.
Run the plugin only where you need it¶
add_filter( 'gallery_custom_links_enabled', function ( $enabled ) {
// Only parse pages that use a gallery.
return is_page( array( 'portfolio', 'partners' ) );
} );
Support another gallery¶
add_filter( 'gallery_custom_links_classes', function ( $classes ) {
$classes[] = '.my-gallery';
return $classes;
} );
Leave the default empty entry in place if you still want every image on the page to be checked; remove it to limit the plugin to the listed galleries, which is faster.
Change a link in code¶
add_filter( 'oggcl_link_url', function ( $url, $media_id ) {
if ( (int) $media_id === 123 && is_user_logged_in() ) {
return home_url( '/members/' );
}
return $url;
}, 20, 2 );
Let editors manage links¶
add_filter( 'oggcl_allow_usage', function ( $allowed ) {
return $allowed || current_user_can( 'edit_others_posts' );
} );
Note
The REST routes are only loaded for users with the administrator role, whatever these filters return.
REST API¶
Namespace: og-gallery-links/v1. Send the X-WP-Nonce header (wp_rest nonce) for logged-in calls.
| Method | Route | Permission | Purpose |
|---|---|---|---|
GET |
/all_settings |
oggcl_allow_setup |
Returns all plugin options. |
POST |
/update_option |
oggcl_allow_setup |
Saves options. JSON body of option => value pairs; only the option names listed below are accepted. Returns all options. |
POST |
/update_meta |
oggcl_allow_usage |
Saves the link fields of one image. JSON body: post_id plus any of the meta keys listed below. |
POST |
/link_settings |
Public | Only registered when the engine is Javascript. Used by the front-end script. |
/link_settings body and reply:
{
"ids": [123, 456],
"ancestors": [ { "key": 0, "id": 123, "classNames": "wp-block-image wp-block-gallery" } ]
}
{
"success": true,
"data": {
"linkSettings": { "123": { "link_url": "https://example.com/", "link_target": "_blank", "link_rel": "", "link_aria": "" } },
"buttons": [ { "key": 0, "html": "" } ]
}
}
buttons is only filled when Use Buttons is on.
Options, meta keys and tables¶
Options (in {prefix}options):
| Option | Default | Setting |
|---|---|---|
oggcl_obmode |
true |
OB Mode |
oggcl_parsing_engine |
HtmlDomParser |
Parsing Engine: HtmlDomParser, DiDom, Javascript or None |
oggcl_log |
false |
Logs |
oggcl_button_enabled |
false |
Use Buttons |
oggcl_button_label |
Click here |
Label |
oggcl_skip_on_current_page |
false |
Skip on Current Page |
oggcl_license |
The saved licence record. |
Attachment meta (in {prefix}postmeta):
| Meta key | Holds |
|---|---|
_gallery_link_url |
Link URL |
_gallery_link_target |
Link Target: _self or _blank |
_gallery_link_rel |
Link Rel |
_gallery_link_aria |
ARIA Label |
_gallery_link_page_links |
Per Page Linkage: an array of ['page' => url, 'target' => url] |
The plugin creates no tables. With a licence, its licensed code is stored in wp-content/og-pro/og-gallery-links/.
Constants¶
| Constant | Value |
|---|---|
OGGCL_VERSION |
Plugin version |
OGGCL_PREFIX |
oggcl |
OGGCL_DOMAIN |
og-gallery-links (text domain) |
OGGCL_ENTRY |
Main plugin file path |
OGGCL_PATH |
Plugin folder path |
OGGCL_URL |
Plugin folder URL |
OGGCL_BASENAME |
Plugin basename |
JavaScript¶
- Admin: the settings screen and the Media Library column read the global
oggcl_gallery_custom_links(script handleoggcl_gallery_custom_links). It holdsapi_url,rest_url,plugin_url,prefix,domain,is_pro,is_registered,rest_nonceand every option above. - Front end, Javascript engine: a footer script defines and runs a global
linkify()function. It finds images with awp-image-IDclass, calls/link_settings, then makes each linked image clickable (or adds the button). - Front end, other engines: a footer script adds one click listener on
windowin the capture phase. It stops other click handlers (lightboxes) on links with the classno-lightboxorset-target, and lets the browser follow the link.
Classes added to the HTML:
| Class | On |
|---|---|
custom-link no-lightbox |
The link around a linked image |
custom-link-button no-lightbox |
The button link when Use Buttons is on |
The page source gets an HTML comment before </body> with the number of images scanned and linked, and the engine used.