'I there a way of changing the text of "add child page" button wagtail
Is there a way to change the text of the button add child page of default Page model of Wagtail?
Solution 1:[1]
Wagtail provides a hooks system to override functionality throughout the admin interface. One of the hooks available is construct_page_listing_buttons which lets you customise the final list of buttons before it renders.
You will need to create a file in any app called wagtail_hooks.py which Wagtail will execute at the start. See - https://docs.wagtail.io/en/stable/reference/hooks.html#hooks
In the source you can see the add page button generation for reference on how they are added.
Example code
wagtail_hooks.py
from wagtail.core import hooks
from wagtail.admin.widgets import Button, PageListingButton
@hooks.register('construct_page_listing_buttons')
def replace_page_listing_button_item(buttons, page, page_perms, is_parent=False, context=None):
for index, button in enumerate(buttons):
# basic code only - recommend you find a more robust way to confirm this is the add child page button
if button.label == 'Add child page':
new_button = Button(...)
buttons[index] = new_button # update the matched button with a new one (note. PageListingButton is used in page listing)
Solution 2:[2]
Minor suggestion to avoid having to re-construct the button, since we are just replacing the text. Should also build this out so that it dynamically changes if the child pages are limited to a single content type.
wagtail_hooks.py
@hooks.register('construct_page_listing_buttons')
def replace_page_listing_button_item(buttons, page, page_perms, is_parent=False, context=None):
for index, button in enumerate(buttons):
# basic code only - recommend you find a more robust way to confirm this is the add child page button
if button.label == 'Add child page':
button.label = 'Add New...'
buttons[index] = button # update the matched button with a new one (note. PageListingButton is used in page listing)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | Peter D Bethke |
